Recently find an internship, in doing test assignment encountered such a problem, on the way to record:
Said, there are 1 to 100 a total of 100 numbers, put into a circle. Starting from 1, every 1, 2, 3, 4 ... The number takes a number, keeps looping, and finally leaves a few? Specifically, the beginning (0 numbers) Take 1 away, 1 number (2) to take 3 away, and then 2 number (4, 5) Take 6 away, and then 3 number (7, 8, 9) take 10 away. After the first lap count to 100 and then from 2 onwards, until the last 1 digits left, what are the last remaining? What if it's 1 to n?
1 Public Static intSelectnumber (intN) {2 3 intresult =-1;4Arraylist<integer> nums =NewArraylist<integer>();5 intindex = 0;//This variable was used to remove numbers from list6 intCount = 0;//count is used to count which numbers should be remove7 intPindex = 0;//This was used to record previous index8 9 //generate a list contains numbers from 1 to nTen for(inti = 1; I <= N; i++) { One Nums.add (i); A } - - while(Nums.size () > 1) { the - while(Index <nums.size ()) { - Nums.remove (index); -count++; +Pindex =index; -Index + =count; + } A atIndex = count-(nums.size ()-pindex); - - while(Index > Nums.size ()-1) { -index = index-nums.size (); - } - } in -result = Nums.get (0); to returnresult; + } - the Public Static voidMain (string[] args) { * intSurviver = Selectnumber (100); $System.out.println ("The Surviver is:" +surviver);Panax Notoginseng}
That's my solution, and the last number left is 31.
There are 1 to 100 a total of 100, starting from 1, every 1, 2, 3 ... Number take one number and the last few?