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.
这么简单的一道题竟然让我做了两三个小时,靠,其实说白了还是语言不会,虽然是算法简单但是却不会用语言实现!!
只是注意怎样判断如果数组到头了怎么继续进行,怎样循环进行了;还有怎样在找出后不再进行终止当前小循环就行了,很简单!!!!!!
开始的超复杂:
#include<iostream>
#include <cstring>
#include <iomanip>
#include<algorithm>
using namespace std;
int main( )
{
int n,x,y,count,xxx,yyy,jishu,aaa,bbb,w;
cin>>n>>x>>y;
while(!(n==0&&x==0&&y==0))
{
int a[25];
memset(a,-1,100);
count=n;xxx=1;//////////记录当前逆时针找人位置
////////////////这样的地方注意初始化的数是几,这道题的话要是初始化为0就在后面特别麻烦
yyy=n; ////////////记录当前顺时针找人位置////////////////
while(count>0)////////圈内剩余人数
{
jishu=0;w=1;while(jishu<=x)////////如果逆时针找的人数不够第x个,就继续查找
{
if(a[xxx]==-1)
{
jishu++;
if(jishu==x&&w==1)
{
aaa=xxx;
w=0;
}
}
xxx++;
if(xxx>n)xxx=1;
}
jishu=0;
w=1;
while(jishu<=y)
{
if(a[yyy]==-1)
{
jishu++;
if(jishu==y&&w==1)
{
bbb=yyy;
w=0;
}
}
yyy--;
if(yyy<=0)yyy=n;
}
xxx--;
if(xxx>n)
xxx=1;
yyy++;
if(yyy<=0)yyy=n;
a[aaa]=a[bbb]=0;
cout<<setfill(' ')<<setw(3);
if(aaa==bbb)
{
cout<<aaa;
count--;
}
else
{
cout<<aaa;
cout<<setfill(' ')<<setw(3);
cout<<bbb;
count=count-2;
}
if(count>0)
cout<<',';
}
cout<<endl;
cin>>n>>x>>y;
}
return 0;
}
#include<iostream>
#include <cstring>
#include <iomanip>
#include<algorithm>
using namespace std;
int main( )
{
int n,x,y,count,xxx,yyy,jishu;
cin>>n>>x>>y;
while(!(n==0&&x==0&&y==0))
{
int a[25];
memset(a,-1,100);
count=n;
xxx=1;//////////记录当前逆时针找人位置////////////////
yyy=n; ////////////记录当前顺时针找人位置////////////////
while(count>0)////////圈内剩余人数
{
jishu=0;
while(1)////////如果逆时针找的人数不够第x个,就继续查找
{
if(a[xxx]==-1)
jishu++;
if(jishu==x)
break;
//注意这个地方特别重要,不然会出现在找出所要找的之后,如果是后面的已经标记为0(即已经剔除列的)
//就还会继续进行这样的话记录的xxx就会出错了
xxx++;
if(xxx>n)
xxx=1;
}
jishu=0;
while(1)
{
if(a[yyy]==-1)
jishu++;
if(jishu==y)
break;
yyy--;
if(yyy<=0)
yyy=n;
}
a[xxx]=a[yyy]=0;
cout<<setfill(' ')<<setw(3);
if(xxx==yyy)
{
cout<<xxx;
count--;
}
else
{
cout<<xxx;
cout<<setfill(' ')<<setw(3);
cout<<yyy;
count=count-2;
}
if(count>0)
cout<<',';
}
cout<<endl;
cin>>n>>x>>y;
}
return 0;
}