Java classic algorithm _ 029 report to 3 people out of the circle

Source: Internet
Author: User
Package WZS. arithmetics; import java. util. tags; // question: there are n people in a circle, ordered by number. When the first person reports the number (from 1 to 3), the person who reports the number 3 leaves the circle and asks the last person who left the number. Public class test_wzs29 {public static void main (string [] ARGs) {system. out. println ("Enter the number of people:"); input = new users (system. in); int member = input. nextint (); // total number of int surplus = member; // number of remaining int COUNT = 0; // report int Index = 0; // The following table indexes Boolean [] array = new Boolean [member]; // The circled for (INT I = 0; I <array. length; I ++) {array [I] = true; // true indicates that the person is still in the circle} // when the remaining number is greater than 1 while (surplus> 1) {If (array [Index] = true) {count ++; // If (COUNT = 3) {array [Index] = false; surplus --; count = 0 ;}} index ++; // when the subscript is equal to the total number of people if (Index = Member) {// re-start number index = 0 ;}} for (INT I = 0; I <array. length; I ++) {If (array [I] = true) {system. out. println ("the remaining persons are:" + I );}}}}

Output result:

 
Enter the number of students: 123. The remaining persons are: 54.

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.