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)