当前位置:网站首页>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;
}
边栏推荐
- node连接mysql数据库报错:Client does not support authentication protocol requested by server
- With 4 years of work experience, the 5 communication methods between multi-threads can't be said, can you believe it?
- log4j-slf4j-impl cannot be present with log4j-to-slf4j
- 伴随着元宇宙、web3.0等概念的兴起,数字人、数字场景等诸多数字化的形态开始出现
- Embedded systems: overview
- 【MySQL进阶】数据库与表的创建和管理
- Adobe是什么?
- Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
- 一个函数有多少种调用方式?
- ML之yellowbrick:基于titanic泰坦尼克是否获救二分类预测数据集利用yellowbrick对LoR逻辑回归模型实现可解释性(阈值图)案例
猜你喜欢

Teach a Man How to Fish - How to Query the Properties of Any SAP UI5 Control by Yourself Documentation and Technical Implementation Details Demo

for loop exercises

noip preliminary round

2019年10月SQL注入的两倍

《数字经济全景白皮书》金融数字用户篇 重磅发布!

override learning (parent and child)

重发布实验报告
![[N1CTF 2018]eating_cms](/img/09/3599d889d9007eb45c6eab3043f0c4.png)
[N1CTF 2018]eating_cms

Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"

2022-08-03 Oracle executes slow SQL-Q17 comparison
随机推荐
Click the icon in Canvas App to generate PDF and save it to Dataverse
for loop exercises
PowerMockup 4.3.4::::Crack
Nine ways to teach you to read the file path in the resources directory
BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
node连接mysql数据库报错:Client does not support authentication protocol requested by server
113. 授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节
优化查询(工作中)
【bug】汇总Elipse项目中代码中文乱码解决方法!
Basic Concepts of Graphs
AOSP CameraLatencyHistogram的原理与使用
Boss: There are too many systems in the company, can you realize account interoperability?
《数字经济全景白皮书》金融数字用户篇 重磅发布!
目标检测的国内外研究现状
Zilliz 2023 秋季校园招聘正式启动!
Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"
.NET6之MiniAPI(十四):跨域CORS(上)
如何基于WPF写一款数据库文档管理工具(二)
代码随想录笔记_动态规划_416分割等和子集
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