The Dole Queue
Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily chosen as number 1, and the rest are numbered counter-clockwise up to N (who will be standing on 1's left). Starting from 1 and moving counter-clockwise, one labour official counts off k applicants, while another official starts from N and moves clockwise, counting m applicants. The two who are chosen are then sent off for retraining; if both officials pick the same person she (he) is sent off to become a politician. Each official then starts counting again at the next available person and the process continues until no-one is left. Note that the two victims (sorry, trainees) leave the ring simultaneously, so it is possible for one official to count a person already selected by the other official.
Input
Write a program that will successively read in (in that order) the three numbers (N, k and m; k, m > 0, 0 < N < 20) and determine the order in which the applicants are sent off for retraining. Each set of three numbers will be on a separate line and the end of data will be signalled by three zeroes (0 0 0).
Output
For each triplet, output a single line of numbers specifying the order in which people are chosen. Each number should be in a field of 3 characters. For pairs of numbers list the person chosen by the counter-clockwise official first. Separate successive pairs (or singletons) by commas (but there should not be a trailing comma).
Sample input
10 4 3 0 0 0
Sample output
4
8,
9
5,
3
1,
2
6,
10,
7
大意:
给出N,K,M三个数字,其中n代表从1到n的人数的的编号,他们逆时针围成一圈,一个人从1逆时针开始数k个人,另一个人从n顺时针开始数m个人;
然后将两个人数到的人拉出队列,继续重复上述步骤,直到所有人都被数出队列,如果两个人数到同一人就输出一个编号;
代码:
#include <cstdio>
int main(){
int n, k, m;
int a[21];
while (scanf("%d%d%d", &n, &k, &m) && n != 0){
getchar();
for (int i = 1; i <= n; i++){
a[i] = i;
}
int cur1 = 1, cur2 = n, num = n, flag;
bool f;
while (num--){
flag = 0;
f = true;
for (int j = cur1, i = 0;; j++){
if (a[j] != 0){
i++;
}
else
flag++;
if (i == k){
cur1 = j;
break;
}
if (j == n)
j = 0;
if (flag == n && i == 0){
f = false;
break;
}
}
if (!f){
break;
}
for (int j = cur2, i = 0;; j--){
if (a[j] != 0){
i++;
}
if (i == m){
cur2 = j;
break;
}
if (j == 1)
j = n + 1;
}
a[cur1] = 0;
a[cur2] = 0;
if (num == n - 1){
if (cur1 == cur2)
printf ("%3d", cur1);
else
printf ("%3d%3d", cur1, cur2);
}
else {
if (cur1 == cur2)
printf (",%3d", cur1);
else
printf (",%3d%3d", cur1, cur2);
}
}
printf("\n");
}
return 0;
}