The original question is this: there is an array of n number, which requires the removal of numbers in odd positions, print out the numbers, the remaining numbers from the new arrangement, continue to remove the numbers in the technical position, and print the numbers; and so on, until the last number is left, to print the numbers on the screen, and displays the position of the last remaining number in the original array.
In fact, this is very regular, with the mathematical solution is very convenient. The idea is that the first removal is: {1,3,5,7 ...} all the odd, the second removal is {1,3,5,7,9 ...} X2, the third time to remove is {1,3,5,7,9}x2^2 and so on, it is easy to write out the program according to mathematical rules.
Unfortunately, the afternoon was hot-headed, trying to count groups, thinking of using the array of sun method to find prime numbers. Results Leng wrote for one hours did not write out, come back calm down, the process of recording down.
First of all, the simple description of my ideas, in fact, directly, is to simulate the process. For example, none of these numbers is 0. Then make a loop that prints one non-zero number at a interval and assigns the number to a new value of 0.
The idea is simple and hard to implement in Java. I wrote a very bad code, hit a lot of patches, the test will still error.
Note: In order to discuss the convenience of the problem, all the numbers as a Boolean array, when the number is removed, this position of the array value is false.
In the beginning I wanted to do two steps at a time loop. In each loop: The first step finds the first true value in the array, turns its value to false, and the second step, finds the second true value in the array, and its value does not change. This writes the code. Very bad, hit a lot of patches, the test will still error. I omitted the code and wrote down my thoughts when I came back.
In fact, the idea is no problem, the key is the operation of the array. An array is a dangerous type, and a slight mistake can be taken over the array length range, resulting in an error, and such errors are difficult to detect. Many methods have been invented to operate the array safely. As a beginner, I have been manipulating arrays all in for (), where the subscript of the previous for is a quiet, safe increment of 1 in each loop, never doing anything else. This time it is different, most of the reasons for the error here.
For this I have summed up a safe operation array subscript method, although it does not seem to look good, but its security and flexibility has been greatly improved.
Its basic structure is as follows:
while (bl[i] = = False) { //while condition i++; Operation of the subscript if (i >=n) { //prevents the value of I for the length of the array ... The following operations can be diversified to initialize the value for the Next Loop }}
Using the for () {}; it is risky to overwrite the underlying value in curly braces, and using a statement similar to the above structure can effectively avoid this risk.
Using this method as the basis, the following is the number of N, the loop to remove the odd position number, the last digit of the implementation code (Java):
int n =100; boolean[] bl = new Boolean[n]; for (int i = 0; i < n; i++) {bl[i] = true; }//initialization, writes all Boolean array values to TRUE. int count = 0; int i = 0; int k = 1; while (count! = n-1) {while (bl[i] = = False) {i++; if (i >=n) {//out of bounds, here means a new loop starts, {} prepares for the next loop i = i% n; System.out.println (); K = 1; }//start from scratch, guaranteed to be found} if (k% 2 = = 1) {Bl[i] = false;//Kill the found number//Syst Em.out.print ((i + 1) + "be Killed\t"); count++; } k++; i++; if (i/n = = 1) {i = i% n; System.out.println (); K = 1; }}//system.out.println (); while (bl[i] = = False) {i++; if (i >=n) {i = i% n; }} System.out.println ("No." + (i + 1) + "personal, not killed"); }
younger brother is also a novice, I hope you have a lot of advice.
An array solution of N-number cyclic odd digit number