学习心得:1.这个有点循环队列的意思。还是采用自顶向下的方法。
2.其实还是搞不清楚啥时候采用自顶向下,百度了一下,说什么把复杂问题变为小问题啊,还有就是什么思维概括,可以把大问题看成同一个的很多小问题。(看来这个问题,还是要自己多做题目,慢慢找到门道)
#include<stdio.h>
#define maxn 25
int n, k, m, a[maxn];
int go (int p, int d, int t); //p表示当前下标,d表示顺时针还是逆时针(顺时针-1,逆时针+1,因为数字是按照逆时针逐渐增加的),t表示要输出第几个
int main(){
while(scanf("%d%d%d", &n, &k, &m) == 3 && n){
for(int i = 1; i <= n; i++){
a[i] = i;
}
int left = n; //还剩余的人数
int p1 = n, p2 = 1; //p1为逆时针的起点,p2为顺时针的起点
while(left){
p1 = go(p1, 1, k);
p2 = go(p2, -1, m);
printf("%3d", p1);
left--;
if(p2 != p1){
printf("%3d", p2);
left--;
}
a[p1] = a[p2] = 0;
if(left)
printf(",");
}
printf("\n");
}
return 0;
}
int go(int p, int d, int t){
while(t--){
do{
p = (p+d+n-1)%n+1; //do中的操作是关于怎样从1到10的边界再回到1,或者反一下。
}while(a[p] == 0); //已经输出了,就再循环一遍,找到没有输出的。
}
return p;
}