There are 1 to 100 a total of 100, starting from 1, every 1, 2, 3 ... Number take one number and the last few?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.