The Dole Queue
题解:
这里写一个走多少步,返回位置的函数真的很重要,并且,把顺时针和逆时针写到了一起,也真的很厉害,需要学习
代码:
#include<stdio.h>
#define maxn 25
int n, k, m, a[maxn];
int go(int p,int d,int t)
{
while(t--){
do{p=(p+d-1+n)%n+1;}while(a[p]==0);
}
return p;
}
int main()
{
while(scanf("%d%d%d", &n, &k, &m) == 3 && n) {
for(int i = 1; i <= 20; i++)a[i] = i;
int left = n;
int p1 = n, p2 = 1;
while(left) {
p1=go(p1,1,k);
p2=go(p2,-1,m);
printf("%3d",p1),left--;
if (p1!=p2) printf("%3d",p2),left--;
a[p1]=a[p2]=0;
if (left)printf(",");
}
puts("");
}
return 0;
}