当前位置:网站首页>PTA monkey chooses King (Joseph Ring problem)
PTA monkey chooses King (Joseph Ring problem)
2022-06-24 09:34:00 【It's a little fish】
Catalog
Application of Joseph Ring formula
subject

Violent solution
At first I realized that this was a Joseph Ring problem , So I thought about whether I could solve the problem violently by marking the array .
The idea at the beginning
- First, I define an array to represent this group of monkeys , The initial values of the array are 1( At the beginning, all the monkeys were in the circle , If the value of an element in the array is 0, It means that the monkey is no longer in the circle )
- Then define a counter ( Indicates the current reported number ), Whenever the number reaches 3 when , Assign the array element value corresponding to the current monkey as 0( It means not in the circle , Note the number of monkeys exiting ), At the same time, the number is reassigned to 0( Start counting again )
- Finally, when the number of monkeys exiting is the total number of monkeys minus one , The king was chosen
The code is as follows :
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i)
a[i] = 1; // At the beginning, the array element values are 1, All monkeys are in the circle
int num = n; // The number of monkeys remaining in the circle
int count = 0; // The serial number of each round of monkeys
int flag = 0; // Monkey King's number
while (num != 1) {
for (int i = 0; i < n; ++i) {
if (a[i] == 1) {
count++;
flag = i;
}
if (count == 3) {
a[i] = 0; // Not in the circle , Start counting again
count = 0;
--num; // To report for duty 3 The monkey of left the circle
}
}
}
System.out.println(flag + 1);
}
}
But when I submit it ( heartache )

Because I can't find the wrong point , I can only change my mind —— At this time, I found that the original problem is the Joseph Ring problem
As for Joseph Ring, as long as we remember the recurrence formula of Joseph Ring on the basis of understanding , This topic becomes simple in an instant
Application of Joseph Ring formula
Let's briefly review the Joseph Ring problem

Finding the last number in a circle can be called the Joseph Ring problem

It means
- f(n,m) The value of means yes 0,1,2,... n - 1, this n The last remaining number after Joseph operation ( That is, the remaining King )
- f(n - 1, m) The value of means yes 0,1,2,... n - 2, this n - 1 The remaining number after Joseph operation
There are the following recurrence formulas

This is recursion ?
If you don't understand Joseph Ring, you can take a look at this article —— Depth analysis of Joseph Ring
The code is as follows
import java.util.*; public class Main { public static int func(int n, int m) { if (n == 1) return 0; int flag = (func(n - 1, m) + m) % n; // flag After Joseph Ring operation , The last remaining number return flag; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int flag = func(n, 3); // flag The serial number of the last monkey after Joseph Ring operation System.out.println(flag + 1); // Because the serial number is from 1 At the beginning , Add one to all } }
But recursion efficiency because there's a round-trip procedure , It's less efficient than direct iteration , You can also iterate from front to back :
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int flag = 0;// Push backward , It is known that the last round of Monkey King is numbered 0, Push to the penultimate round , Count down three rounds ... All the way to the initial position . //flag It means the last monkey ( Monkey King ) In each round ( Count down to the last 1 round , Find the penultimate round ,...) Own serial number in //i Means the last i round for (int i = 2; i <= n; ++i) { // there i It is equivalent to... In recursion n,3 Namely m flag = (flag + 3) % i; // flag The serial number of the last monkey after Joseph Ring operation // The number of monkeys in the current circle changes dynamically , Recursion is when n be equal to 1 Exit from time , We from n==2 The process of traversing from front to back is equivalent to a recursive operation } System.out.println(flag + 1); // Because the serial number is from 1 At the beginning , Add one to all } }
边栏推荐
- Webrtc series - network transmission 5: select the optimal connection switching
- 198. 打家劫舍
- What do you mean by waiting for insurance records? Where should I go for filing?
- 软件系统依赖关系分析
- Framework tool class obtained by chance for self use
- LeetCode之最长公共前缀
- [GDB debugging tool] | how to debug under multithreading, multiprocessing and running programs
- Longest public prefix of leetcode
- The ambition of JD instant retailing from 618
- Zero foundation self-study SQL course | related sub query
猜你喜欢

零基础自学SQL课程 | HAVING子句

【bug】@JsonFormat 使用时出现日期少一天的问题

R 椭圆随机点产生并画图

医学图像开源数据集汇总(二)

Netrca: an effective network fault cause localization

Ggplot2 color setting summary

Summary of medical image open source datasets (II)

Learning Tai Chi Maker - esp8226 (XIII) OTA

When programmers are asked if they can repair computers... | daily anecdotes

PTA猴子选大王(约瑟夫环问题)
随机推荐
转:三星电子CEO:一切决策都要从认清自己开始
Directly applicable go coding specification
Get post: do you really know the difference between requests??????
2022.6.13-6.19 AI行业周刊(第102期):职业发展
【ES6闯关】Promise堪比原生的自定义封装(万字)
【输入法】迄今为止,居然有这么多汉字输入法!
深入解析 Apache BookKeeper 系列:第三篇——读取原理
零基础自学SQL课程 | SQL语句语法顺序与执行顺序
Leetcode-- string
Microblog writing - flow chart - sequence chart - Gantt chart - Mermaid flow chart - good results
Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
When to use RDD and dataframe/dataset
leetcode--链表
Codeforces Round #392 (Div. 2) D. Ability To Convert
读CVPR 2022目标检测论文得到的亿点点启发
ApplicationContextInitializer的三种使用方法
LeetCode之最长公共前缀
谈谈数字化转型晓知识
Oracle的tnsnames.ora文件配置
threejs的点光源+环境光