報數游戲是這樣的:有n個人圍成一圈,按順序從1到n編好號。從第一個人開始報數,報到m(<n)的人退出圈子;下一個人從1開始報數,報到m的人退出圈子。如此下去,直到留下最后一個人。本題要求編寫函數,給出每個人的退出順序編號。其中n是初始人數;m是游戲規定的退出位次(保證為小於n的正整數)。函數CountOff將每個人的退出順序編號存在數組out[]中。因為C語言數組下標是從0開始的,所以第i個位置上的人是第out[i-1]個退出的。
#include <stdio.h>
#define MAXN 20
void CountOff( int n, int m, int out[] );
int main()
{
int out[MAXN], n, m;
int i;
scanf("%d %d", &n, &m);
CountOff( n, m, out );
for ( i = 0; i < n; i++ )
printf("%d ", out[i]);
printf("\n");
return 0;
}
void CountOff( int n, int m, int out[] )
{
int i=0,count=0,cnt=0;
for(int i=0;i<n;i++)
out[i]=-1;
while(count!=n)
{
if(out[i]==-1)
cnt++;
if(cnt==m)
{
count++;
out[i]=count;
cnt=0;
}
if(i==n-1&&count!=n)
i=0;
else
i++;
}
return ;
}

