Problem description:
This is a classic problem. For example, if there are 6 people, the first person starts to report data from 1, and the numbers reported by the next person increase in sequence, when the reported number is a certain number, the reported person goes out and the game continues. The number of people who have not been out of the game continues from 1 until all the people have been out of the game. Obtain the order of exit.
For example, there are 6 people, respectively 1, 2, 3, 4, 5, 6. If the number of people reported to 3 goes out, the order of departure should be: 3, 6, 4, 2, 5, 1
Solution:
You can set the flag bit of the array to 0 to indicate that the array is out. When it is traversed, the count does not need to be increased. It is also a common idea to set the flag position in actual development.
Code:
#include <stdio.h>/*total people number*/#define ALL 100/*people leave when count to left_counter*/#define left_counter 3/*people array*/int people[ALL];/*init people array*/void initPeople(){int i = 0 ;for (i = 0; i < ALL; i++){people[i] = i+1;}}/*print people array*/void printPeople(){int i = 0;for (i = 0; i < ALL; i++){printf("%d ",people[i]);}printf("\n");}int main(void){initPeople();int left = ALL;/*init total left number*/int counter = 0;/*init counter*/int i = 0;/*init array index*/while (1){if (0 != people[i]){counter++;}/*if counter == left_counter , peopler out, set people[i] = 0 counter = 0; left--;**/if (counter == left_counter){left--;printf("%d is out\n", people[i]);counter = 0;people[i] = 0;printPeople();}/*if no people left, problem solved*/if (0 == left){printf("problem finished!\n");break;}/*increase index*/i++;if (i == ALL){i = 0;}}return 0;}
Joseph PHUs problem beginner (using arrays)