java-data structures and algorithms-every 3 minus 1

Source: Internet
Author: User

1. Requirements: A group of people in a circle to count, every 3 back 1 people, asked to calculate the last left of the subscript of the People

2. Code:

1  PackageTest;2 3  Public classcount3quit1 {4 5     //requirements: A group of people in a circle to count, every 3 back 1 people, asked to calculate the last left of the subscript of the People6     7      Public Static voidMain (string[] args) {8         9         //receive Java parameters specify number of peopleTen         intLen = Integer.parseint (args[0]); One          A         //simulates a circle of people with a Boolean array, True indicates still, false indicates exited -         Boolean[] Peoples =New Boolean[Len]; -         /*For (Boolean b:peoples) { the B = true; -         }*/ -          for(inti = 0; i < peoples.length; i++){ -Peoples[i] =true; +         } -          +          A         //every 3 back 1 at         intLeftcount = Len;//1.leftCount says how many people are left now -         intindex = 0;//2.index Current Element subscript -         intCount = 0;//3.count means how many to count -          -          while(Leftcount > 1){ -             if(Peoples[index]) { incount++; -                 if(Count = = 3){ toPeoples[index] =false; +Count=0; -leftcount--;//minus 1 for the rest of the population. the                 } *             } $             Panax Notoginseng             //put the element under the label -index++; the              +             //If you have counted to the end of the array, start the number A             if(Index > Len-1){ theindex = 0; +             } -         } $          $          for(inti = 0; i < peoples.length; i++){ -             if(Peoples[i]) { - System.out.println (i); the             } -         }Wuyi     } the}

3. Running results Java COUNT3QUIT1 500:

435

java-data structures and algorithms-every 3 minus 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.