An array of shapes, all of which are positive integers, such as 2,1,3,4,5,7, now require the deletion of a number per m number, to the end of the array, and then to the array header, and so on until all the number of the array is deleted, the last number of the deleted in the array of the original subscript.
For example: For an array of arr = [2,1,3,4,5,7], if M = 2 o'clock, the entire delete process (with the deletion marked as-1) is as follows:
Array of elements: [2,1,3,4,5,7]
First time: [2, 1, 3,-1, 5, 7]
Second time: [-1, 1, 3,-1, 5, 7]
Third time: [-1, 1, 3,-1,-1, 7]
Fourth time: [-1, 1,-1,-1,-1, 7]
Fifth time: [-1, 1,-1,-1,-1,-1]
Sixth time: [-1,-1,-1,-1,-1,-1]
Therefore, the last number to be deleted is 1, and its subscript in the array is 1.
The specific code, implemented as follows (Java edition):
1 ImportJava.util.Scanner;2 /**3 * Array number of circular delete problem, an array, for example, 2,1,3,4,5,7, every 2 number of deleted, deleted to the tail and then loop to the header, so repeatedly, until the last number is deleted4 * Output the subscript for the last deleted number, 15 * @authorJiajoa6 * Input includes 3 lines, first row n is the length of the array, second behavior n positive integers, separated by spaces, third behavior interval m7 * For example:8 * 69 * 2 1 3 4 5 7Ten * 2 One * Output: 1 A */ - Public classRecurrnumber { - the Public Static voidMain (string[] args) { -Scanner in =NewScanner (system.in); - intn = in.nextint ();//an array of length n - int[] arr =New int[n]; + for(inti=0;i<n;i++){ -Arr[i] =in.nextint (); + } A at intm = In.nextint ();//Delete one number per interval for M - intindex =GetResult (arr,m); - System.out.println (index); - in.close (); - } - in Public Static intGetResult (int[] arr,intm) { - intLen =arr.length; to intStart = 1; + intCount=0; - intFlag =m; the while(flag>0){ * if(count==len) $ Break;Panax Notoginseng if(start>=Len) { -Start = start%Len; the } + if(Arr[start]!=-1){ Aflag--; the } +start++; - if(flag==0){ $ if(start>=Len) { $Start = start%Len; - } - while(Arr[start]==-1){ thestart++; - if(start>=Len) {WuyiStart = start%Len; the } - } WuArr[start]=-1; -count++; Aboutflag=m; $ } - } - returnstart; - } A}
To iterate over an element in an array