1. 有N个人围成一圈,顺序排号,从第一个人开始报数,凡是报到M的人退出圈子,问最后留下的几号?
#define N 15
int main(int argc, char const *argv[])
{
int M;
scanf("%d",&M);
int people[N]={0};
int people_remain = N;
int people_out = 0;
int people_count = 1;
int i = 0;
while(people_remain!=1){
if (0 == people[i]){
if(M == people_count){
people[i] = 1;
--people_remain;
++people_out;
people_count=1;
}
else
++people_count;
}
i=(i+1)%N;
}
for (int i = 0; i < N; ++i)
{
if (people[i] == 0)
printf("%d\n",i+1);
}
return 0;
}
2. N个人围成一圈,从第S个人开始顺时针1,2,3,4,...M的顺序报数,数到M的人出圈,然后从出圈的下一个人开始重复此过程,输出所有出圈的人的顺序。
int main(int argc, char const *argv[])
{
int M,S,N;
printf("please input people number\n");
scanf("%d",&N);//总人数
printf("please input counting cycle\n");
scanf("%d",&M);//报数循环
printf("please input counting start people\n");
scanf("%d",&S);//从第几个人开始
int *people =(int *)calloc(N,sizeof(int));
int people_remain = N;
int people_out = 0;
int people_count = 1;
int i = S-1;
printf("出圈人的顺序为:");
while(people_remain!=1)
{
if (0 == people[i])
{
if(M == people_count)
{
printf("%d,",i+1);
people[i] = 1;
--people_remain;
++people_out;
people_count=1;
}
else
++people_count;
}
i=(i+1)%N;
}
printf("\n");
for (int i = 0; i < N; ++i)
{
if (people[i] == 0)
printf("%d\n",i+1);
}
return 0;
}
** N个人围成一圈,从第1个人开始顺时针1,2,3,4,...M的顺序报数,数到M的人出圈,然后从出圈的下一个人开始重复此过程,直到圈中所剩的人数为M-1个人。输出所有出圈的人的顺序,并输出剩余的人的编号**
int main(int argc, char const *argv[])
{
int M,N;
printf("please input people number\n");
scanf("%d",&N);
printf("please input counting cycle\n");
scanf("%d",&M);
int *people =(int *)calloc(N,sizeof(int));
int people_remain = N;
int people_count = 1;
int i = 0;
printf("出圈人的顺序为:");
while(people_remain!=M-1)
{
if (0 == people[i])
{
if(M == people_count)
{
printf("%d,",i+1);
people[i] = 1;
--people_remain;
people_count=1;
}
else
++people_count;
}
i=(i+1)%N;
}
printf("\n");
printf("剩下的人的编号为:");
for (int i = 0; i < N; ++i)
{
if (people[i] == 0)
printf("%d,",i+1);
}
printf("\n");
return 0;
}