A group of people were playing the game of "three times out of the circle" and asked the last person to stay who was in the first place?

The meaning of this question is: there are n students reporting in a circle (1 ~ 3). The students who report to 3 quit the circle, and the remaining students continue to report until there is one left. Ask the remaining students what the original serial number is?

For example: seven students are playing this game

 @        @         @         @         @         @         @

1 # 2 # 3 (retreat) 1 # 2 # 3 (retreat) 1

2 # 3 (retreat) 1 # 2 # 3 (retreat)

1 # 2 # 3 (retreat)

 1                                     2

3 (return) remaining

So the last remaining student first ranked fourth, that is, the serial number is four.

When I saw this problem, I thought of using the idea of recursion. Then it is necessary to write a function that can realize this function, call it repeatedly, print out the serial number of this person when there is only one person left, and end the recursion.

So to write a function that implements such a function, we must first consider what parameters are required for this function?

First, when a group of people are playing the game, we can use the array to realize it and explain how large the array is (that is, how many students are playing at the beginning).

Then it is because this is to start counting from the first person every time. After traversing the game, record how many people reported three and let them quit. Then subtract the number of people who quit after reporting the number this time from the total number to get the remaining number. Until there is only one person left, directly output the serial number of this person.

Finally, because each time starts from the first person, and the first person reports one for the first time, but not necessarily every time after that, there should be a parameter record to tell the first person how many times to report in each round.

So the function header is as follows:

void func(int a[],int n,int tap,int num);
//n is the total number of people at the beginning
//tap is used to record how many people are left after three times of retreat,
//num is used to record the number of people who start counting every time (1 / 2 / 3)

Now let's write the implementation of the function

//The students who check in 3 quit the circle, and the remaining students continued to count in,
//tap is used to record how many people are left after each 3-day return,
//num is used to record the number of people who start counting every time (1 / 2 / 3)
void func(int a[],int n,int tap,int num)
{
	int i,j=0;
	if(tap==1&&n==1)        //At the beginning, there was only one student, so there was no need to retreat
	{
		printf("Only one person,Don't back off!\n");
	}
	else if(tap==1&&n!=1)        //Finally, there was only one classmate left
	{
		//Traversal the remaining number that is not the minimum value is in which bit
		for(i=0;i<n;i++)
		{
			if(a[i]!=INT32_MIN)
			{
				break;
			}
		}
		printf("The original serial number of the last person left is:%d\n",i+1);
	}
	else
	{
        //Assign the minimum value int32 to the students who check in 3 every time_ MAX
        //Then, in the next count, if it is the minimum value, skip it directly
		for(i=0;i<n;i++)
		{
			if(a[i]!=INT32_MIN)
			{
				num++;    
			}
			if(num==3)
			{
				a[i]=INT32_MIN;
				flag++;        //flag is used to record how many people quit each time. flag is a global variable
				num=0;        //After reporting 1 ~ 3, the number starts from 1, so set num to 0
			}
		}
		func(a,n,n-flag,num);    //n-flag is the person left after each time
	}
}

Finally, the overall code (plus the main function) is as follows:

Here's an INT32_MAX, which is the minimum value of int type. To use it, you need a header file #include < stdint h>

#include <stdio.h>
#include <stdint.h>
#define MAX 100
int flag=0;        //global variable
void func(int a[],int n,int tap,int num);    //Function declaration
int main(void)
{
	int a[MAX]={0};        //Initialize all arrays to 0
	int i,n;
	printf("Please enter the size of the array:");    
	scanf("%d",&n);            //Enter the number of people playing the game from the keyboard
	printf("Please enter the value of the array element:");
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);    //You can assign each student a value, which is arbitrary, as long as it is not the minimum value
	}
	int num=0;
	func(a,n,n-flag,num);
	return 0;
}
void func(int a[],int n,int tap,int num)
{
	int i,j=0;
	if(tap==1&&n==1)
	{
		printf("Only one person,Don't back off!\n");
	}
	else if(tap==1&&n!=1)
	{
		//Traversal the remaining number that is not the minimum value is in which bit
		for(i=0;i<n;i++)
		{
			if(a[i]!=INT32_MIN)
			{
				break;
			}
		}
		printf("The original serial number of the last person left is:%d\n",i+1);//Because the subscript of the array starts from 0, you need + 1
	}
	else
	{
		for(i=0;i<n;i++)
		{
			if(a[i]!=INT32_MIN)
			{
				num++;
			}
			if(num==3)
			{
				a[i]=INT32_MIN;
				flag++;
				num=0;
			}
		}
		func(a,n,n-flag,num);
	}
}

The above is the idea of this programming problem!! Xiaobian will update some interesting programming problems and some knowledge points in time. Thank you for your browsing. I hope you can get your recognition. If there are any deficiencies, you are also welcome to leave messages and comments.

Keywords: C C++ Game Development

Added by mimcris on Wed, 26 Jan 2022 11:55:46 +0200