信息奥赛一本通—编程启蒙(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

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

相关推荐
小小杨树2 小时前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE21218 小时前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE21218 小时前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术1 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦1 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
clint4561 天前
C++进阶(1)——前景提要
c++
用户497863050731 天前
(一)小红的数组操作
算法·编程语言
夜悊1 天前
C++代码示例:进制数简单生成工具
c++
怕浪猫1 天前
Electron 系列文章封面图
算法·架构·前端框架