[Programming questions] The delete has an array of a[n] order to store 0~n-1, requiring every two number to delete a number, to the end of the loop to the beginning of the continuation of the last deleted number of the original subscript position. Take 8 numbers (n=7) as an example: {0,1,2,3,4,5,6,7},0->1->2 (delete)->3->4->5 (delete), 6->7->0 (delete), so loop until the last number is deleted.
Input Description:
Each set of data is one line of an integer n (less than or equal to 1000), the number of members of the array, and if greater than 1000, the a[999] is evaluated.
Output Description:
The original subscript position of the last deleted number line output.
Input Example:
8
Output Example:
6
Ideas:
ImportJava.util.*; Public classmain{ Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); Do{ intn =Sc.nextint (); int[] arr =New int[n]; for(inti=0;i<n;i++) Arr[i]=i; Final intDelflag = n + 1;//Remove Flag bit intCurrentSize = n;//records the current effective length of the array (that is, the number of elements that are not set to the delete flag), and finally becomes 0 Final intSTEP = 2;//Step Size intCount = 0;//Step Count intLastdelindex = 0;//Record the subscript of the last deleted element inti = 0;//cyclic subscript while(CurrentSize! = 0) { if(Arr[i]! = Delflag) {//To interpret whether the current element equals a delete flag if(count++ = = STEP) {//when the step count satisfies the step sizeArr[i] = Delflag;//To place an element as a delete flag bitLastdelindex = i;//record the subscript of the placecurrentsize--;//effective array length minus 1Count = 0;//Step Count Zeroing//System.out.println ("Deleted index is" + i% n); }} I= (i + 1)% n;//Subscript take-over to achieve cyclic subscript} System.out.println (Lastdelindex); } while(Sc.hasnext ()); }}
Huawei Machine Test 4[programming question] Delete number