习题8-4 报数

 1 void CountOff(int n, int m, int out[])
 2 {
 3 
 4     int person[n + 1];
 5     int i;
 6     int count;
 7 
 8     /*
 9         给编号1到n的人打上标签
10         =0在圈子中
11         =1退出圈子
12     */
13     for (i = 1; i <= n; i++)
14     {
15         person[i] = 0;
16     }
17 
18     i = 1;
19     count = 0; //小循环计数
20     int cnt = 0;
21     while (cnt < n)
22     {
23         if (i == n + 1)
24         {
25             i = 1;
26         }
27         if (person[i] == 0)
28         {
29             count++;
30             if (count == m)
31             {
32                 cnt++;
33                 out[i - 1] = cnt;
34                 count = 0;
35                 person[i] = 1;
36             }
37         }
38         i++;
39     }
40 }

猜你喜欢

转载自www.cnblogs.com/2018jason/p/12071899.html