您的位置:首页 > 产品设计 > UI/UE

UVA133 The Dole Queue

2014-02-03 23:55 393 查看
Background

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

where

represents a space.

解题思路:

可以用数组模拟圆圈,然后逆时针,顺时针遍历,有点类似约瑟夫环,用flag[ ]来标志改元素是否已被遍历过,然后注意输出格式!

附上代码:

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
int flag[22];
int main()
{
int N,K,M,n;
int i,count,first,second,temp=0;
while(cin >> N >> K >> M)
{
temp=0;//temp记录被带走的人数
if(N==0&&K==0&&M==0)//结束输入的判断
break;
for(i=1;i<=N;i++)
flag[i]=1;//标志
n=N;
first=1;
second=N;
while(n)
{
count=0;
for(i=first;;i++)//i=first,并不是每一次遍历都是从1开始的,而是上一回合结束的地方开始
{
if(flag[i]==1)
count++;
if(count==K)
{
flag[i]=0;//置为0表示,该位置的人已被带走
first=i;
cout << setw(3) << first;//宽度为3
temp++;
break;
}
if(i==N)
i=0;
}
count=0;
for(i=second;;i--)
{
if(flag[i]==1||i==first)//注意:有可能逆时针和顺时针带走的是同一个人
count++;
if(count==M)
{
flag[i]=0;
second=i;
if(second!=first)
{
cout << setw(3) << second;
temp++;
}
break;
}
if(i==1)
i=N+1;
}
//对n进行减去带走的人数,当n=0时结束while
if(first==second)
n--;
else
n-=2;
if(temp<N)
cout << ",";
else
cout << endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: