Test algorithm training report number

Resource limit
Time limit: 1.0s Memory limit: 64.0MB


Problem description
  There are n students standing in a circle, numbering 1 to n clockwise. Start with student number 1 and report 1/2 clockwise. Students who register for 1 stay in place, and students who register for 2 exit the circle until there is only one student left. Ask the last remaining student number.


The input format
  is only one line, a positive integer n.
Output format
  only one line, a positive integer.


Sample input
400
Sample output
289


Data scale and convention
  n<=2000000


Method 1:

Problem solving method:

  • Use the queue queue in C++ to solve:

push() insert an element at the end of the
queue pop() delete the first element of the queue
size() return the number of elements in the queue
empty() return true if the queue is empty
front() return the first element in the queue
back() Return to the last element
in the queue Reprinted in: C++ queue queue usage details

  • See the program notes for details.

Source program:

#include<iostream>
#include <queue>//对应的头文件
using namespace std;
queue<int>m;
void init(int n)//对应的编号,并存入队列
{
    
    
   for (int i = 1; i <= n; i++)
   	m.push(i);
}
void print(int n)
{
    
    
   int flag = 1;//报数
   while (!m.empty())
   {
    
    
   	if (flag == 2)//当报数报到2时
   	{
    
    
   		m.pop();//将对应的编号出队
   		flag = 1;//重置报数
   	}
   	else
   	{
    
    
   		m.push(m.front());//将头元素入队
   		m.pop();//然后再出队
   		flag++;//报数增加
   	}
   	if (m.size() == 1)//当剩下最后一人时,就是没有被淘汰的编号
   	{
    
    
   		cout << m.front() << endl;
   		break;
   	}
   }
}
int main()
{
    
    
   int n;
   cin >> n;
   init(n);
   print(n);
}

supplement:

Users can specify by themselves, such as one to three rounds of reporting, and the person reporting 2 or 3 is eliminated, and only a slight modification to the source program is required. The question of reporting the number is simpler than that of Joseph.

Evaluation results:

Insert picture description here

Method 2:

Problem solving method:

If you think about this question in reverse, what he asked is the last person, that is, the last person to be eliminated. Why
don't you start with the first person, and then increase to so many people, his position is the last person. s position

Reprinted at: https://blog.csdn.net/a1439775520/article/details/105853821/

Source code

#include<iostream>
using namespace std;
int main()
{
    
    
	int n;
	cin >> n;
	int sum = 0;
	for (int i = 2; i <= n; i++)
		sum = (sum + 2) % i;
	cout << sum+1  << endl;
}

Evaluation results:

Insert picture description here

  1. It can be found that the two problem-solving methods have a large difference in space complexity, and the second method is more optimized, but it is relatively difficult to understand. The two methods can be selected and used by themselves.
  2. Finally, the problem of Joseph ring can be derived from the problem of reporting the number. I will explain the problem of Joseph ring later, and those who are interested can pay attention to it.

Guess you like

Origin blog.csdn.net/weixin_49243150/article/details/113338393