当前位置:网站首页>[pta-- use queues to solve the problem of monkeys choosing kings]
[pta-- use queues to solve the problem of monkeys choosing kings]
2022-07-28 06:40:00 【Step by step b】
Problem description
A group of monkeys are going to choose a new monkey king . The choice of the new monkey king is : Give Way N There are only monkeys in a circle , The sequence number from a certain position is 1~N Number . From 1 We'll start counting on the , From 1 To report for duty 3, Where to report 3 That's the monkey who quit the circle , And then from the next monkey next to the same number . And so on and on , The last monkey left is the monkey king . What's the original Monkey King number ?
Input format :
Enter a positive integer on a line N(≤1000).
Output format :
Output the number of monkey king in one line .
sample input :
11
sample output :
7analysis
We know that queues have the characteristics of first in, first out , If you put the first position of the queue back in the queue , Then the number of the first position becomes the last number of the new queue , Thus, a closed loop can be formed . Therefore, using queues can better solve this kind of problem .
According to the problem description , A group of monkeys form a circle , Every time the third monkey is eliminated , The last one is the king to choose . According to example analysis , We can create a queue first , take 11 Monkeys are put into the queue in turn , Then just look at the first three monkeys in the queue , Remove the first two monkeys from the original queue , Then put it back in the queue , After the third monkey is removed, there is no need to care , Thus cycle , When there is only one element left in the queue , Out of the loop , This element is the number to choose .
for the first time :

The second time :

third time :
The fourth time :

The fifth time :

The sixth time :

In turn, cycle , Finally, there is only one element left :

That is to say 7, namely 7 It's the monkey king .
c++ The code is as follows :
#include<iostream>
#include<queue>
using namespace std;
int main() {
int n;
cin>>n;
queue<int> que;
for (int i = 1; i <= n; i++) {
que.push(i);
}
while (1) {
if (que.size() == 1) break;
for (int i = 1; i < 3; i++) {
int t = que.front();
que.pop();
que.push(t);
}
que.pop();
}
cout<<que.front();
}边栏推荐
- OJ 1020 minimum palindromes
- 刷题记录----哈希表
- OJ 1129 fraction matrix
- Leetcode brush question diary sword finger offer II 053. Medium order successor in binary search tree
- OJ 1284 counting problem
- 2022-05-24 use of spiel
- 【动态规划--买卖股票的最佳时期系列3】
- feignclient @RequestMapping参数设置及请求头简易方式设置
- 刷题记录----二叉树的层序遍历
- What's a good gift for your girlfriend on the Chinese Valentine's day in 2022? Practical and beautiful gift recommendation
猜你喜欢

中国剩余定理 个人理解
![OpenGL development environment configuration [vs2017] + frequently asked questions](/img/29/cefa8601310caf56ae9605cbf7fbbf.png)
OpenGL development environment configuration [vs2017] + frequently asked questions

做气传导耳机最好的是哪家、最好的气传导耳机盘点

2022年七夕礼物推荐!好看便宜又实用的礼物推荐

气导贴耳式耳机推荐、不需要佩戴入耳的气传导耳机
![[C note] data type and storage](/img/3d/6b7a848dff5a8c0ccd0a54c19bce46.png)
[C note] data type and storage

What's a good gift for your girlfriend on Chinese Valentine's day? Boys who can't give gifts, look!

【动态规划--买卖股票的最佳时期系列3】

什么气传导蓝牙耳机好、配置比较高的气传导耳机推荐

What are the open earphones? Four types of air conduction earphones with excellent sound quality are recommended
随机推荐
OJ 1020 minimum palindromes
项目编译NoSuch***Error问题
C语言的动态内存管理函数
费马小定理
七夕送什么礼物好?小众又高级的产品礼物推荐
OJ 1020 最小的回文数
Network communication and tcp/ip protocol
Feignclient @requestmapping parameter setting and simple method setting of request header
中国剩余定理 个人理解
STM32的IAP跳转相关bug经历
[C note] data type and storage
QT batch operation control and set signal slot
2022-07-17 Damon database installation
[pta ---- traversal of tree]
OJ 1507 删数问题
Valgrind tool
Antenna effect solution
什么气传导蓝牙耳机好、配置比较高的气传导耳机推荐
SSAO By Computer Shader(一)
OJ 1284 counting problem