当前位置:网站首页>7-28 monkeys choose King (Joseph problem)
7-28 monkeys choose King (Joseph problem)
2022-07-03 14:14:00 【Big fish】
subject :
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 input :
7
#include<stdio.h>
int main()
{
int i,j,k,temp;
int m[1001];
int n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
m[i]=i+1;
}
for(i=n-1;i>=0;i--)
{
for(k=1;k<=3;k++)
{
temp=m[0];
for(j=0;j<i;j++)
{
m[j]=m[j+1];
}
m[i]=temp;
}
}
printf("%d",m[0]);
return 0;
}
Method resolution :
The array subscript
0 1 2 3 4 5 6 7 8 9 10
The original sequence
1 2 3 4 5 6 7 8 9 10 11
New sequence
2 3 4 5 6 7 8 9 10 11 1
3 4 5 6 7 8 9 10 11 1 2
4 5 6 7 8 9 10 11 1 2 3 ——3 That is, the first monkey to quit ( Stay in the array ).
A new round
5 6 7 8 9 10 11 1 2 4 3
6 7 8 9 10 11 1 2 4 5 3
7 8 9 10 11 1 2 4 5 6 3 ——6 That is, the second monkey to quit .
Continue to cycle , Subscript to the last array 0 The serial number of is the last monkey .
summary : This topic is the classic Joseph Ring problem , At first I saw that I had no way to start ( Novice programmer ), Referring to the information on the Internet , There are some clever solutions . Interested partners can refer to . Link address :https://blog.csdn.net/yanweibujian/article/details/50876631
边栏推荐
- Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
- Redis:Redis的数据结构、key的操作命令
- 常见问题之PHP——ldap_add(): Add: Undefined attribute type in
- QT learning 21 standard dialog box in QT (Part 2)
- Leetcode (4) -- find the median of two positively ordered arrays
- Redis: operation command of string type data
- Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email
- Raft agreement
- QT learning 17 dialog box and its types
- Exercise 8-7 string sorting
猜你喜欢
7-7 12-24 hour system
Cross linked cyclodextrin metal organic framework loaded methotrexate slow-release particles | metal organic porous material uio-66 loaded with flavonoid glycosides | Qiyue
7-8 overspeed judgment
Solution to failure or slow downloading of electron when electron uses electron builder to package
Exercise 10-2 recursive factorial sum
JVM runtime data area
Article content typesetting and code highlighting
jvm-类加载
中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
Exercise 10-6 recursively find Fabonacci sequence
随机推荐
How to bold text in AI
7-11 calculation of residential water charges by sections
The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
QT learning 19 standard dialog box in QT (top)
js . Find the first palindrome string in the array
Exercise 10-1 judge the three digits that meet the conditions
必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
FPGA测试方法以Mentor工具为例
JS first summary
Invalid Z-index problem
全局事件总线
6-9 statistics of single digits (15 points)
Folic acid modified metal organic framework (zif-8) baicalin loaded metal organic framework composite magnetic material (AU- [email
Vite project commissioning
泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
中国PETG市场预测及战略研究报告(2022版)
Common mixins
28: Chapter 3: develop Passport Service: 11: define attributes in the configuration file, and then obtain them in the code;
MongoDB索引