标签:c语言
#include <stdio.h>
#include <stdlib.h>
int LastNum(int n)
{
int i = 0;
int out = 0; //退出的人数
int num = 0; //报数
int lastnum = 0;
int *a;
if(n <= 0){
printf("Person num is illegal\n");
return -1;
}
a=(int*)malloc(n*sizeof(int));
for (i = 0; i < n; i++)
{
a[i] = 1;
}
i = 0;
while (out != n-1)
{
if (a[i] == 1)
{
num++;
}
if (num == 3)
{
a[i] = 0;
num = 0;
out++;
}
i++;
if (i == n)
{
i = 0;
}
}
for (i = 0; i < n; i++)
{
if (a[i] == 1)
{
lastnum = ++i;
break;
}
}
return lastnum;
}
void main()
{
int n;
printf("Please input total person number:");
scanf("%d", &n);
printf("The last person number is %d\n", LastNum(n));
}
标签:c语言
原文地址:http://whatyouwant.blog.51cto.com/10579929/1688438