坐圈数数踢人游戏C语言设计,排成一圈数3退出最后一人位置问题--C语言谭浩强版练习8.5...

有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号位置

/******************************************************

E8.5

Plan:可以不循环喊1到3,用总数是不是3的倍数代替

CREATE----------------------------

By:     Idooi Liu

Time:   2015/10/15-1500

----------------------------------

******************************************************/

#include 

#include 

int expThree(int n, int manArray[]);

int main(void)

{

int nMan;       //How many people

char *pMan;     //The array

int siteLast;   //The last person's site

int i;

printf("Please input how many people:\n");

scanf("%d", &nMan);

pMan=malloc(nMan*sizeof(char));

//Assignment

for(i=0; i

*(pMan+i)='Y';

siteLast=expThree(nMan, pMan);

printf("The last one\'s site is %d\n", siteLast);

free(pMan);

return 0;

}

int expThree(int n, int manArray[])

{

int i, j;

int siteLast;   //The last person's site

int flag=0;     //Count off

int k;

for(i=0; i

{

k=0;

//For check how many people last

for(j=0; j

{

if(manArray[j]=='Y')

k++;

siteLast=j;

}

if(k==1)        //Only one person

break;

if(manArray[i]=='Y')

{

flag++;     //The person who in the team take the next number

//If the person's number is divisible by 3, take out of the team

if(flag%3==0)

manArray[i]='N';

}

}

return siteLast;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值