山东省科研平台:那位帮帮忙 用指针做出这道题啊~~~

来源:百度文库 编辑:神马品牌网 时间:2024/05/08 13:21:24
有几个人围成一圈,顺序排号,从第一个开始报数,(从1到3报数),凡报到"3"的人退出圈子,最后留下的是几号?
用指针做出来,或者指针以前的内容。

#include <stdio.h>
#include <malloc.h>
#include <memory.h>
//pLeft长度固定为N, 表示队伍中留下人的位置.nLeave是离开的人数, 判断结束
//输出是依次从队伍中离开的人的序号.
int fun(unsigned char *pLeft, int N, int *nLeave, int m, int nStart)
{
int nCount=0,nPoint=nStart;
if(pLeft[nPoint]==1)
nCount++;
while(nCount<m)
{
nPoint=nPoint%N+1;
if(pLeft[nPoint]==1)
nCount++;
}
(*nLeave)++;
pLeft[nPoint]=0;
return nPoint;
}

void main(int argc, char *argv[])
{
int n=0,m=0,nLeave=0,nStart=1;
printf("输入 人数n,上限m.\n");
scanf("%d,%d",&n,&m);
unsigned char *pLeft=(unsigned char *)calloc(n+1,sizeof(char));
memset(pLeft,1,(n+1)*sizeof(char));
while(nLeave<n)
printf("%d\t",nStart=fun(pLeft, n, &nLeave, m, nStart));
free(pLeft);
}

---------输出,20个人,m=26------------
输入 人数n,上限m.
20,26
6 13 1 11 3 17 14 12 16 2
10 8 15 5 9 18 20 7 4 19