Number three fallback problem algorithm (Java)

Source: Internet
Author: User

Number three the question is, there is a circle of children, holding hands in a circle, starting from the first child number 1, the second child number 2, the third child number 3, this time the number of 3 of children quit, starting from the next child to Count 1, has been circulating, until the end of a child, ask the child's position?

Two ways to solve the problem, one is to think of this group of children as an array (assuming there are 500 arrays), each element is a Boolean, the initial time all elements are true, and then start the number of loops, determine whether the remaining elements are greater than 1, first of all to determine whether the element is true,true to continue the number, Each time you count to 3 o'clock, the number of remaining elements is recorded, and the number is zeroed so that you can start counting again from 0.

public class count3quit1{

public static void Main (string[] args) {

Boolean[] kids;

Kids = new boolean[500];

for (int i = 0; I <kids.length; i++) {

Kids[i] = true;

}

int leftnum = Kids.length;

int countnum = 0;

int index = 0;

while (Leftnum > 1) {

if (Kids[index]) {

countnum++;

}

if (countnum==3) {

Kids[index] = false;

Countnum = 0;

leftnum--;

}

index++;

if (Indedx = = 500) {

index = 0;

}

}

for (int i = 0;i < Kids.length; i++) {
if (Kids[i]) {
System.out.println (i);
}
}

}

}

Number three fallback problem algorithm (Java)

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.