应该是约瑟夫问题,但是此题数据范围比较小,所以可以用暴力枚举解决。难得一道一遍过的题目。。。
可以看到,这道模拟题的每一阶段就是每一轮的顺时针逆时针查找,考虑到这是一个环,并且不知道会对环有多少次循环,所以用递归解决
1 #include "bits/stdc++.h" 2 using namespace std; 3 const int MAX=25; 4 int n,k,m,t; 5 bool a[MAX]; 6 int work1(int pos,int num){ 7 if (a[pos]) num++; 8 if (num==k) return pos; 9 if (pos==n) return work1(1,num); 10 else return work1(pos+1,num); 11 } 12 int work2(int pos,int num){ 13 if (a[pos]) num++; 14 if (num==m) return pos; 15 if (pos==1) return work2(n,num); 16 else return work2(pos-1,num); 17 } 18 int main(){ 19 freopen ("queue.in","r",stdin); 20 freopen ("queue.out","w",stdout); 21 int i,j,x,y; 22 while (scanf("%d%d%d",&n,&k,&m),n){ 23 memset(a,true,sizeof(a)); 24 t=n;x=1,y=n; 25 while (t){ 26 x=work1(x,0); 27 y=work2(y,0); 28 if (x!=y){ 29 if (t==n) printf("%3d%3d",x,y); 30 else printf(",%3d%3d",x,y); 31 t-=2; 32 } 33 else{ 34 if (t==n) printf("%3d",x); 35 else printf(",%3d",x); 36 t--; 37 } 38 a[x]=a[y]=false; 39 } 40 printf(" "); 41 } 42 return 0; 43 }