The problem of multi-person circle reporting in C language programming

Description of the problem: There are n people in a circle and they are numbered in order. Start counting from the first person (counting from 1 to 3), all the people who report to 3 leave the circle and ask who is the original number left at the end.

Problem analysis: the for loop is used in combination with while, and the effect is remarkable.

Program [source code] :

#include<stdio.h>
#define nmax 50
void main()
{
int i,k,m,n,num[nmax],*p;
printf("please input the total of numbers:");
scanf("%d",&n);
p=whether;
for(i=0;i<n;i++)
 *(p+i)=i+1;
 i=0;
 k=0;
 m=0;
 while(m<n-1)
 {
 if(*(p+i)!=0) k++;
 if(k==3)
 { *(p+i)=0;
 k=0;
 m++;
 }
i++;
if(i==n) i=0;
}
while(*p==0) p++;
printf("%d is left\n",*p);
}

Leave a Comment

Your email address will not be published. Required fields are marked *