信息奥赛一本通—编程启蒙(3345:【例60.2】 约瑟夫问题)

代码如下:

cpp 复制代码
#include <iostream>
using namespace std;
int main() {
	int n, m;
	cin >> n >> m;
	bool people[105] = {true}; 
	for (int i = 1; i <= n; i++) {
		people[i] = true;
	}
	int count = 0;   
	int num = 0;     
	int x = 0;    
	while (count < n) {
		x++;      
		if (x > n) { 
			x = 1;
		}
		
		if (people[x] == true) {  
			num++;
			if (num == m) {
				cout << x << " ";  
				people[x] = false;  
				count++;              
				num = 0;              
			}
		}
	}
	
	return 0;
}

网站链接:https://bas.ssoier.cn/problem_show.php?pid=3345

创作不易,给个关注吧🦀🦀

相关推荐
小灰灰搞电子10 小时前
Rust+Slint 实现动态消息提示框源码分享
开发语言·后端·rust
神仙别闹11 小时前
基于 C++ 实现两个有序链表序列的交集
java·c++·链表
传奇开心果编程11 小时前
【Rust入门知识点学与练】第21课:Trait 进阶 Advanced Traits
开发语言·学习·rust
新时代牛马11 小时前
字符设备驱动完整篇:从 cdev_add、file_operations 到chrdev_open 与排障
开发语言·python
swordbob12 小时前
ReentrantLock 与 AQS 完整学习手册
java·开发语言
政企项目老覃12 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
淡海水12 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
白山编程大哥12 小时前
Java OutputStreamWriter 详解:从字符到字节的桥梁
java·开发语言·python
hansang_IR12 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论