Joseph Ring Problem_Array Solution_C Language

1_Title

There are n people forming a circle and starting to count. The m people who have reported are out and will no longer participate in the counting, and so on until all n people are out.

2_Analysis

1. Instantiate abstract problems

Suppose there is a referee:

mouth: value range 1~m (the person who reports m is out)

finger: value range 1~n (there are n people in total)

8372c8531fe5457e92ff67a502253dba.png

2. Assign values ​​to n and m, and personal simulation report

hypothesis

n==8    ,  m==5

Then the order of those who are out is: 5 2 8 7 1 4 6 3

3. Find problems from personal simulation reporting

The code is as follows: (Details are explained in the code comments)

int n=0;  //参加报数的总人数 
int m=0;  //报到m的人出局

scanf("%d",&n);
scanf("%d",&m); 
//输入完毕 

int table[1005]={0};  //数据规模可以开大一些 
for(int i=1;i<=n;i++)  //将数组中的人先全部标号为 1 ,之后出局的人标号为 0 
{
	table[i]=1;
 } 
 
 //开始操作
 int mouth=0;  //1~m    //mouth==m怎么办? 
 int finger=0;  //1~n   //table[finger]==0  怎么办? 

It is easy to find that the key to the problem is:

Ⅰ.What to do if mouth==m?

Ⅱ.table[finger]==0 What to do?

After thinking clearly about the above two questions, this problem will be solved.

4. Solve problems

Specific operations in the code (it is useless to say more, solve the problem in the problem):

 int cnt=n; //cnt需要实现--的操作,以结束代码 
 while(cnt!=0)  //表示还有人在参加报数
 {
 	mouth++;
 	finger++;
 	while(table[finger]==0)  //如果指到空位了的操作 
 	{
 		finger++;  //不管这个空位,finger继续++ 
 		if(finger>n)  //finger超过了总人数 
 		{
 			finger=1;  //重新将finger赋值为  1 
		 }
	 }
	 if(mouth==m)  //有人报到m这个数了,需要出局 
 	{
 		printf("%d ",finger);  //打印出局的人的编号
 		table[finger]=0;  //将出局的人标号为  0  ,表示不再参加报数 
		 cnt--;  //参加报数的人 --
		 mouth = 0;  //更新 mouth 的值 
	 }
 	
  } 

Three_Summary

There are many ways to solve the Joseph Ring problem:

For example, recursion, linked list , etc.

This article only introduces the simplest one (array). You must not belittle yourself when learning this article. The ocean of knowledge is bottomless and no one can see the whole picture.

All the codes are attached below for you to take a look at, and please let me know if there are any deficiencies !

#include<stdio.h>
int main()
{
  
int n=0;  //参加报数的总人数 
int m=0;  //报到m的人出局

scanf("%d",&n);
scanf("%d",&m); 
//输入完毕 

int table[1005]={0};  //数据规模可以开大一些 
for(int i=1;i<=n;i++)  //将数组中的人先全部标号为 1 ,之后出局的人标号为 0 
{
	table[i]=1;
 } 
 
 //开始操作
 int mouth=0;  //1~m    //mouth==m怎么办? 
 int finger=0;  //1~n   //table[finger]== 0  怎么办? 
 int cnt=n; //cnt需要实现--的操作,以结束代码 
 while(cnt!=0)  //表示还有人在参加报数
 {
 	mouth++;
 	finger++;
 	while(table[finger]==0)  //如果指到空位了的操作 
 	{
 		finger++;  //不管这个空位,finger继续++ 
 		if(finger>n)  //finger超过了总人数 
 		{
 			finger=1;  //重新将finger赋值为  1 
		 }
	 }
	 if(mouth==m)  //有人报到m这个数了,需要出局 
 	{
 		printf("%d ",finger);  //打印出局的人的编号
 		table[finger]=0;  //将出局的人标号为  0  ,表示不再参加报数 
		 cnt--;  //参加报数的人 --
		 mouth = 0;  //更新 mouth 的值 
	 }
 	
  }
  return 0;
}

 

Thanks for watching!

 

 

 

 

 

Guess you like

Origin blog.csdn.net/nmbg11/article/details/134958816