当前位置:网站首页>P1996 约瑟夫问题
P1996 约瑟夫问题
2022-08-03 22:51:00 【Recursi】
题目描述
n n n 个人围成一圈,从第一个人开始报数,数到 m m m 的人出列,再由下一个人重新从 1 1 1 开始报数,数到 m m m 的人再出圈,依次类推,直到所有的人都出圈,请输出依次出圈人的编号。
注意:本题和《深入浅出-基础篇》上例题的表述稍有不同。书上表述是给出淘汰 n − 1 n-1 n−1 名小朋友,而该题是全部出圈。
输入格式
输入两个整数 n , m n,m n,m。
输出格式
输出一行 n n n 个整数,按顺序输出每个出圈人的编号。
样例 #1
样例输入 #1
10 3
样例输出 #1
3 6 9 2 7 1 8 5 10 4
提示
1 ≤ m , n ≤ 100 1 \le m, n \le 100 1≤m,n≤100
/* * @Description: To iterate is human, to recurse divine. * @Autor: Recursion * @Date: 2022-08-02 20:21:04 * @LastEditTime: 2022-08-02 21:29:25 */
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;
const int N = 1e6;
int n,m;
bool a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
int pos = 0;
cin >> n >> m;
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= m; j++){
pos ++;
if(pos> n)
pos = 1;
if(a[pos]==1){
j--;
}
}
cout << pos << " ";
a[pos] = 1;
}
return 0;
}
/* * @Description: To iterate is human, to recurse divine. * @Autor: Recursion * @Date: 2022-08-02 21:40:07 * @LastEditTime: 2022-08-02 21:45:02 */
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;
const int N = 1e6;
int n,m;
queue<int> q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
int num = 1;
for(int i = 1;i <= n;i ++)
q.push(i);
while(!q.empty()){
if(num == m){
cout << q.front() << " ";
q.pop();
num = 1;
}
else{
num++;
q.push(q.front());//排至队尾
q.pop();
}
}
return 0;
}
边栏推荐
- Codeup刷题笔记-简单模拟
- pikachu Over permission
- 静态文件快速建站
- Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
- Codeup brushing notes - simple simulation
- utils 定时器
- V8中的快慢数组(附源码、图文更易理解)
- 斩获双奖|易知微荣获“2021中国数字孪生解决方案优秀供应商”“中国智能制造优秀推荐产品”双奖项!
- 电商秒杀系统
- websocket多线程发送消息报错TEXT_PARTIAL_WRITING--自旋锁替换synchronized独占锁的使用案例
猜你喜欢

ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset

"Digital Economy Panorama White Paper" Financial Digital User Chapter released!

Boss: There are too many systems in the company, can you realize account interoperability?

冰河又一MySQL力作出版(文末送书)!!

【bug】汇总Elipse项目中代码中文乱码解决方法!

Embedded Systems: Clocks

封装、包、访问权限修饰符、static变量

最小化安装debian11

2022的七夕,奉上7个精美的表白代码,同时教大家快速改源码自用

易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
随机推荐
LabVIEW code generation error 61056
什么是memoization,它有什么用?
[RYU] rest_router.py source code analysis
Why do we need callbacks
October 2019 Twice SQL Injection
Kotlin - 扩展函数和运算符重载
The sword refers to the offer question 22 - the Kth node from the bottom in the linked list
override学习(父类和子类)
嵌入式系统:GPIO
亿流量大考(2):开发一套高容错分布式系统
HCIP BGP lab report
Websocket multi-threaded sending message error TEXT_PARTIAL_WRITING--Use case of spin lock replacing synchronized exclusive lock
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
封装、包、访问权限修饰符、static变量
"Digital Economy Panorama White Paper" Financial Digital User Chapter released!
[MySQL Advanced] Creation and Management of Databases and Tables
encapsulation, package, access modifier, static variable
for loop exercises
直播预告 | 构建业务智联,快速拥抱财务数字化转型
BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记