当前位置:网站首页>PTA猴子选大王(约瑟夫环问题)
PTA猴子选大王(约瑟夫环问题)
2022-06-24 08:12:00 【是小鱼儿哈】
目录
题目

暴力求解
一开始我每意识到这是一个约瑟夫环问题,于是就想着能不能通过对数组标记的方法暴力求解。
一开始的思路
- 首先我定义一个数组表示这群猴子,数组的初始值都为1(表示一开始所有的猴子都在圈子中,如果数组中某个元素的值为0,则表示这个猴子不再圈子中)
- 接着定义一个计数器(表示当前的所报的号数),每当号数达到3时,就把当前的猴子所对应的数组元素值赋值为0(表示不在圈子中,注意记录退出的猴子的个数),同时号数重新赋值为0(重新开始报数)
- 最后当退出的猴子个数为猴子总个数减一时,就选出来了大王
代码如下:
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; // 一开始数组元素值都为1,表示当前所有的猴子都在圈子中
int num = n; // 圈子中剩余猴子的个数
int count = 0; // 每轮猴子的序号
int flag = 0; // 猴王的编号
while (num != 1) {
for (int i = 0; i < n; ++i) {
if (a[i] == 1) {
count++;
flag = i;
}
if (count == 3) {
a[i] = 0; // 不在圈子中,重新开始报数
count = 0;
--num; // 报到3的那只猴子退出圈子
}
}
}
System.out.println(flag + 1);
}
}
但当我提交上去(扎心了)

由于我一直找不到那个出错的点,我只能换一种思路——这时我才发现原来这道题这就是约瑟夫环问题
而对于约瑟夫环问题只要我们在理解基础上记住约瑟夫环的递推公式,这道题目瞬间就变得简单起来了
约瑟夫环公式的应用
我们来简单回顾一下约瑟夫环问题

求圈圈里的最后一个数就可以被称为约瑟夫环问题

意思就是
- f(n,m)的值就表示对0,1,2,... n - 1,这n个数做约瑟夫操作后最后剩下的那个数(即剩下的那个大王)
- f(n - 1, m)的值就表示对0,1,2,... n - 2,这n - 1个数做约瑟夫操作后剩下的那个数
有以下递推公式

这不就是递归吗?
如果对约瑟夫环不理解可以看一下这篇文章——约瑟夫环深度解析
代码如下
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表示经过约瑟夫环操作后,最后剩余的那个数字 return flag; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int flag = func(n, 3); // flag表示约瑟夫环操作后最后一只猴子的序号 System.out.println(flag + 1); // 因为序号是从1开始的,所有要加一 } }
但是递归效率因为有个来回的规程,效率相比直接迭代差一些,也可从前往后迭代:
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;//倒推,已知最后一轮猴大王编号为0,推得倒数第二轮,倒数三轮。。。一直到初始位置。 //flag表示最后一只猴子(猴大王)在每轮(先倒数第1轮,再求倒数第二轮,...)中自己的序号 //i表示倒数第i轮 for (int i = 2; i <= n; ++i) { // 这里的i就相当于递归中的n,3就是m flag = (flag + 3) % i; // flag表示约瑟夫环操作后最后一只猴子的序号 // 当前圈子的猴子数量是动态变化的,递归是当n等于1时退出,我们从n==2开始从前向后遍历的过程就相当于做了递归操作 } System.out.println(flag + 1); // 因为序号是从1开始的,所有要加一 } }
边栏推荐
- The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year
- Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems
- eBanb B1手环刷固件异常中断处理
- leetcode--链表
- 荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
- Niuke network decimal integer to hexadecimal string
- CF566E-Restoring Map【bitset】
- 零基础自学SQL课程 | 子查询
- [GDB debugging tool] | how to debug under multithreading, multiprocessing and running programs
- 小白学习MySQL - 增量统计SQL的需求
猜你喜欢

Redis implements a globally unique ID

Linux MySQL installation

CF566E-Restoring Map【bitset】

零基础自学SQL课程 | 子查询

2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3

Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?

PhpStrom代码格式化设置

Lu Qi: I am most optimistic about these four major technology trends

解决:jmeter5.5在win11下界面上的字特别小

从618看京东即时零售的野心
随机推荐
Linux (centos7.9) installation and deployment of MySQL Cluster 7.6
MySQL data (Linux Environment) scheduled backup
【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试
Code written by mysql, data addition, deletion, query and modification, etc
CDGA|到底怎么才能做好数据治理呢?
Go 语言项目开发实战目录
[noi Simulation Competition] send (tree DP)
普通人没有学历,自学编程可以月入过万吗?
198. house raiding
获取带参数的微信小程序二维码-以及修改二维码LOGO源码分享
Epidemic situation, unemployment, 2022, we shouted to lie down!
小白学习MySQL - 增量统计SQL的需求
Target of cmake command_ compile_ options
深入了解 border
L01_ How is an SQL query executed?
【输入法】迄今为止,居然有这么多汉字输入法!
Lu Qi: I am most optimistic about these four major technology trends
2020 China's provinces and cities, three-level linkage data, data agencies (data from the official website of the National Bureau of Statistics)
When to use RDD and dataframe/dataset
PHP封装一个文件上传类(支持单文件多文件上传)