思路:记录下总的人数,每次走人时都减去,作为大循环的条件,当总人数为0的时候跳出循环;大循环内套两个循环,一个顺时针走,一个逆时针最后输出即可
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a[25],n,k,m,i,j,sum,flagk,flagm,flagi,flagj;
while(~scanf("%d%d%d",&n,&k,&m))
{
sum=n;j=n-1;
for(i=0;i<25;i++) a[i]=1;
i=0;
if(n==0&&k==0&&m==0) break;
flagk=k;flagm=m;
while(n>0)
{
while(1)
{
if(a[i]==1)
flagk--;
if(flagk==0)
{
flagi=i;
flagk=k;
break;
}
i++;
if(i==sum) i=0;
}
while(1)
{
if(a[j]==1) flagm--;
if(flagm==0)
{
flagm=m;
a[j]=0;
a[flagi]=0;
flagj=j;
if(flagi==flagj) n-=1;
else n-=2;
break;
}
j--;
if(j<0) j=sum-1;
}
if(flagi==flagj) printf("%3d",flagi);
else printf("%3d%3d",flagi,flagj);
if((n==2&&flagi==flagj)||(n==1))
continue;
else printf(",");
printf("\n**%d**\n",n);
}
}
return 0;
}