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.
为了减少失业人口数量,新国家绿港犀牛党制定了下面的策略。每天失业救济金的申请者将要面朝里围成一个大圈。随意挑选一个人作为第1号,剩余的人以逆时针为序依次编号到N(N即1号的左边一位)。一个劳动部门的官员从1号开始以逆时针方向计数到第k个申请者,另一位官员从N号开始以顺时针方向计数到第m个申请者。这两个被选中的人将被选出去再培训;如果两个官员选中了同一个人,那么这个被选中的人将被选出去当官。然后每个官员从下一个有效位置开始继续计数,重复这个过程直到没有一个人剩下。注意那两个不幸的家伙(将要进行再培训的人)要同时离开大圈,因此有可能一个官员点到一个已经被另一个官员选中的人。
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).
写一个程序依次读入3个数(N,k,m; k,m > 0, 0 < N < 20),然后决定要被送去再培训的人的顺序。每3个数写在单独的一行,以3个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).
对每一次输入,输出一行数字代表被选中的人的顺序。每个数字占3个字符。每一对数字逆时针计数的官员选中的人在前,相继的数对以逗号隔开,最后一个逗号省略。
Sample input
10 4 3 0 0 0
Sample output
4 8, 9 5, 3 1, 2 6, 10, 7
where represents a space.
#include <stdio.h>
#include <string.h>
int main()
{
int applicants[30];
int flag[30];
for(int i=1;i<=30;i++)
{
applicants[i-1]=i;
}
int N,k,m,count,countk,countm,posk,posm,isFirst;
while(scanf("%d%d%d",&N,&k,&m))
{
if(N==0 && k==0 && m==0) break;
memset(flag,0,sizeof(flag));
isFirst=1;
count=0;
posk=0;
posm=N-1;
while(count<N)
{
countk=0;
while(countk<k)
{
if(posk==N)
posk=0;
if(flag[posk]==0)
countk++;
posk++;
}
countm=0;
while(countm<m)
{
if(posm==-1)
posm=N-1;
if(flag[posm]==0)
countm++;
posm--;
}
if(posk-1==posm+1)
{
flag[posk-1]=1;
if(isFirst)
{
printf("%3d",applicants[posk-1]);
isFirst=0;
}
else
printf(",%3d",applicants[posk-1]);
count++;
}
else
{
flag[posk-1]=flag[posm+1]=1;
if(isFirst)
{
printf("%3d%3d",applicants[posk-1],applicants[posm+1]);
isFirst=0;
}
else
printf(",%3d%3d",applicants[posk-1],applicants[posm+1]);
count=count+2;
}
}
printf("\n");
}
return 0;
}