Reports for n people in one circle, reports for one circle

Source: Internet
Author: User

Reports for n people in one circle, reports for one circle
Question: There are n people in a circle with sequential troubleshooting. Report number from the first person (report number from 1 to 3). When a person reports to the third person, leave the circle and ask the person who left the last number.

Idea: use an array to store the n people. The initial state is set to 1, indicating that they are still in the circle.

Then, q is used to record the number of the report, because every time the report number is reported to 3, the person has to exit the circle and j is used to represent the remaining number of people in the circle.

Whenever someone Reports 3, J-1, and the value of the element in the array is marked as 0, q clear 0.

In this way, after the while loop ends, only one person's value in the array will be 1, and the serial number of the person will be output + 1 (the array starts from 0, so + 1)

 

1 # include <stdio. h> 2 int main () {3 int a [100]; 4 int n; 5 int I, j; 6 printf ("Enter the number of participants in the game: "); 7 scanf (" % d ", & n); 8 j = n; 9 for (I = 0; I <n; I ++) 10 a [I] = 1; 11 int q = 0; // number of record steps 12 while (j> 1) {13 for (I = 0; I <n; I ++) {14 if (a [I]! = 0) q ++; 15 if (q = 3) {16 a [I] = 0; 17 j --; 18 q = 0; 19 printf ("% d player out of the circle \ n", I + 1); 20} 21} 22} 23 printf ("\ n "); 24 for (I = 0; I <n; I ++) {25 if (a [I] = 1) printf ("the last player in the circle is the % d player", I + 1); 26} 27}

 

Run:

 

Related Article

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.