In fact, this question is not difficult, that is, the output requirements are a little higher. As a result, I have learned a lot, resulting in a long and messy code, resulting in wa and pe.
Question Analysis: If you want to open an array and simulate a ring, you can directly use an array to simulate a circular linked list + Delete. OK. An error occurred while understanding the output. As a result, WAF performed 3 times and peat twice. Here is a small trap:
If the point to be deleted is exactly the point where another person starts counting again, move one digit forward or backward.
The following code is used:
#include<stdio.h>#define MAXN 22int queue[MAXN];int prio[MAXN],next[MAXN];int n,k,m,N;void initi(){ for(int i=1;i<=n;i++) { queue[n-i+1]=i; if(i==1) prio[n-i+1]=1; else prio[n-i+1]=n-i+2; if(i==n) next[n-i+1]=n; else next[n-i+1]=n-i; }}int couter_clock(int begin){ for(int i=1;i<k;i++) begin=prio[begin]; return begin;}int clock(int begin){ for(int i=1;i<m;i++) begin=next[begin]; return begin;}void del(int begin){ prio[next[begin]]=prio[begin]; next[prio[begin]]=next[begin]; N--;}int main(){ while(scanf("%d%d%d",&n,&k,&m),n,k,m){ initi(); int begin1=1; int begin2=n; N=n; while(true) { int ans1=couter_clock(begin1); int ans2=clock(begin2); begin1=prio[ans1]; begin2=next[ans2]; del(ans1); if(ans1!=ans2) del(ans2); if(begin1==ans2) begin1=prio[begin1]; if(begin2==ans1) begin2=next[begin2]; printf("%3d",ans1); if(ans1!=ans2) printf("%3d",ans2); if(N>0) printf(","); else {printf("\n");break;} } } return 0;}
Hey, although the question is simple, the coding capability is improved! Keep moving!
Zys love sky and shadow!