Count 3, return 1, count 3

Source: Internet
Author: User

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 }}}}

 

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.