Number 3 back 1

Source: Internet
Author: User

I found some Java videos on the internet and learned a bit. The teacher told a question, there are 500 children pulled into a circle, beginning 123123 Such a count, the number to 3 of the children out of the circle, asked: The last child's position is the number.

I followed the teacher's thinking wisp, and then I blind dozen code, feeling very complex, so the code copy down, left for later review with. (Results: 435)

//Number 3 back 1 Public classcount3quit1 { Public Static voidMain (string[] args) {boolean[] arr=Newboolean[ -];  for(inti =0; i < arr.length; i++) {Arr[i]=true; }        intLeftcount = Arr.length;//number of remaining children initialized        intCountnum =0;//Counter (number 3 back 1) Initialize        intindex =0;//Subscript Value Initialization         while(Leftcount >1){            if(Arr[index] = =true) {Countnum++; if(Countnum = =3){//number of counters to 3Arr[index] =false;//from true to false means to let the child quitCountnum =0;//Counter 0Leftcount--;//number of children remaining-1}} Index++;//Subscript Value            if(index = = arr.length) {//(0-499) When the mark is No. 500, a circle cycle is complete, start again cycle, initialize subscriptindex =0; }                    }         for(inti =0; i < arr.length; i++){            if(Arr[i] = =true) {System. out. println (i);//Print the location of the last Child Left            }        }    }    }

Number 3 back 1

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.