[learning notes] C language exercise: n people in a circle, arranged in sequence. Start counting from the first person (from 1 to 3). Anyone who reports 3 withdraws from the circle and asks the person who left the last number.

Time:2020-12-23

On the first day of the freshman year, do the exercises after class. (Chapter 8, question 5, Chapter 8, C programming, 5th Edition, by Tan Haoqiang)

The specific title is shown in the title. First of all, I want to use an array to represent n individuals. First, the first n bits are initialized to 1, and the loop number exits from the third bit. The exit is represented by 0. When there is only the last person left, the number of digits will be output.

In the loop, there are several points to consider:

1. When counting to the third digit, assign 0 to the array element pointed to by the pointer.

2. Pointer should skip 0.

3. After the pointer points to the last bit, it should be reset to the front of the first bit.

The code is as follows:

#include 
 #include 
 #Define size 1024 // maximum number of judges
 int main(){
     int numberLast,peopleNum,peopleExist[SIZE]={0},i;
     Int * firstptr, * moveptr; // firstptr points to the first position of the judgment array peopleexist
 
     puts("Enter the number of people.");
     scanf("%d",&peopleNum);
 
     movePtr=firstPtr=peopleExist;
     numberLast=peopleNum;
     //Initialization of personnel existence judgment
     for(i=0;i

The results are as follows:

Enter the number of people.
5
The last people is No.4.