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: