c语言 报数

来源:百度知道 编辑:UC知道 时间:2024/07/05 20:51:09
n个人围成一圈,1,2,3循环报数,报到3的人退出。到最后只余1人,输出最后留下的是第几号(最初的序号,以1起始)。若n=6,则输出1;若n=10,则输出4;若n=100,则输出91。函数int fun(int n)实现上述功能,返回n个人中最后余的1人的起始序号。
int fun(int n)
{
}
void main()
{int m;
scanf("%d",&m);
printf("n=%d\n",fun(m));
}

帮帮忙,各位高手。

#include <stdio.h>
#include <conio.h>
int fun(int n)
{
int i = 0, p, tmp;

while( ++i <= n )
{
p = i * 3;
while (p > n)
p = p - n + (p - n - 1)/(3 - 1);
tmp=p;
}
return tmp;
}

void main()
{int m;
scanf("%d",&m);
printf("n=%d\n",fun(m));
}

//这个题目任何c上都有

#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[])
{
in