原文网址:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=832&page=show_problem&problem=69
也真是醉了,本来看起来简单的一道题,非要搞很久才做得出来,不知道题目太纠结了,还是自己基础知识掌握的不扎实......
就是一群人围成一个圈,从一个起点开始,顺时针和逆时针数数,数到某个数字时停止,最后两种方式数到的数字可能有一个或者两个,然后输出(重复的不输出),求出这样规则的出队顺序.......
用的数组来模拟,感觉真不爽啊........很奇怪的感觉,最需要注意的就是边界的处理,以及输出的下标和操作的下标的对应关系。
#include<stdio.h> #include<string.h> int n,k,m,x[25]; int count(int bg,int turn,int step)//计数 { int cnt=0,i=bg; while(cnt<turn) { if(step<0&&i==0)//到达最小的边界的话,返回到右边界 { i=n; } i+=step;//step 这个参数可以控制循环的方向 if(x[i%n]) { ++cnt; } } return i%n;//返回的下标 } void slove() { memset(x,-1,sizeof(x)); int cnt=0,left=-1,right=n;//起始位置也是纠结了很久 while(cnt<n) { if(cnt)//控制格式 { printf(","); } left=count(left,k,1);//第一种计数 right=count(right,m,-1);//第二种计数 printf("%3d",left+1); x[left]=0;//对应的下标清零 ++cnt; if(right!=left)//只有不同的才输出 { printf("%3d",right+1); ++cnt; x[right]=0; } } printf("\n"); return ; } int main() { //freopen("shuju.txt","r",stdin); while(scanf("%d%d%d",&n,&k,&m),n|m|k) { slove(); } return 0; }
也是写完了才发现,好像可以用双端队列模拟试试看,应该会简便点,醉了......