Count 3, return 1, count 3
I found some JAVA videos on the Internet and learned about them. The teacher gave a question, where 500 children were pulled into a circle and started to report the number as 123123. When the number of children reached 3, they left the circle and asked: Where is the last child.
I followed the teacher's ideas and blindly typed the code. It was very complicated, so I copied the code and left it for later review. (Result: 435)
// Count 3 return 1 public class Count3Quit1 {public static void main (String [] args) {boolean [] arr = new boolean [500]; for (int I = 0; I <arr. length; I ++) {arr [I] = true;} int leftCount = arr. length; // initialization of the remaining number of children int countNum = 0; // counter (count 3 return 1) initialization of int index = 0; // The value of the lower mark is initialized while (leftCount> 1) {if (arr [index] = true) {countNum ++; if (countNum = 3) {// count the number of counters to 3 arr [index] = false; // changing from true to false means that the child is exited countNum = 0; // the counter is set to 0 leftCount --; // number of remaining children-1} index ++; // if (index = arr. length) {// (0-499) when the current mark is 500th, the cycle is completed and the next cycle starts. The initial subscript index = 0 ;}for (int I = 0; I <arr. length; I ++) {if (arr [I] = true) {System. out. println (I); // print the location of the last child }}}}