50 men circled in multiples of 3 and 3, and asked who the rest were? In the original position is how much (easy to understand)

Source: Internet
Author: User

The number of elements in the circle of the topic is 50, each time a multiple of 3 or 3, the current element out of the loop, and continue to count (assuming that the number of 6 is deleted, then the next one is called 7 instead of the 6) until you encounter a multiple of 3. Here, if the subscript starts from 0, until the lap is complete, it will receive the first lap, how should this be handled? In fact, the best way is to use the method of redundancy, you can always get 3 multiples, no matter how many multiples it is, regardless of its number of elements.

Because each time the element is removed, the number of elements will be less one, so the next 3 multiples actually only need to walk two steps, in order to assign a value for its subscript, need to subtract one, keep each removed element is a multiple of 3.

Note: If you start with a 0-based subscript, you should use -1 (corresponding to the subsequent index-- ) (make the starting number 123, initialize-1, The next number to be removed is 3). If the subscript 0 is deleted, subscript 1 has not been deleted, this time subscript 1 of the number of the subscript automatically 0.

public class Cycletest {public static void main (string[] args) {//TODO auto-generated method StubSystem.out.println ("The digital original The location is: "+ cycle (50,3));}  private static int cycle (int total, int. k) {list<integer> dataList = new linkedlist<integer> (); for (int i = 0; I < total; i++) {Datalist.add (new Integer (i+1));} int index = -1;while (Datalist.size () >1) {index = (index + k)%datalist.size ();d atalist.remove (index--);//Remove the number, Index minus one displaylist (dataList);//Print after clear, observe the result System.out.println ();} Return Datalist.get (0); You can also replace the next line of return ((Integer) datalist.get (0)). Intvalue (); private static void Displaylist (List<integer> dataList) {for (int i = 0; i < datalist.size (); i++) {int a = Datal Ist.get (i); System.out.print (A + "");}}}


You can easily test, such as Test 5 number, when the number is less than the time to understand the principle.

Reference article: http://blog.csdn.net/hi_kevin/article/details/17678239

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

50 men circled in multiples of 3 and 3, and asked who the rest were? In the original position is how much (easy to understand)

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.