王道约瑟夫问题
#include<queue>
#include<cstdio>
using namespace std;
int main(){
int n, p , m;
while(true){
scanf("%d%d%d", &n, &p, &m);
if(n == 0 && p == 0 && m == 0)
break;
}
queue<int> children;//队列中的元素是孩子的编号
for(int i = p, j = 0; j < n; ++j) {
//i用来记录 孩子编号
//j用来记录已经遍历的孩子数量
children.push(i);
++i;
if(i > n){
i = 1;
}
}
//喊号过程
int num = 1;
while(true){
int cur = children.front();//cur是队首孩子的编号
children.pop();//喊完了,对手出队
if(num == m){
num = 1;
if(children.empty()){
printf("%d\n", cur);
break;
}else{
printf("%d\n", cur);
}
} else{
num = num + 1;
children.push(cur);
}
}
}
#include<cstdio>
using namespace std;
int main(){
int n, p , m;
while(true){
scanf("%d%d%d", &n, &p, &m);
if(n == 0 && p == 0 && m == 0)
break;
}
queue<int> children;//队列中的元素是孩子的编号
for(int i = p, j = 0; j < n; ++j) {
//i用来记录 孩子编号
//j用来记录已经遍历的孩子数量
children.push(i);
++i;
if(i > n){
i = 1;
}
}
//喊号过程
int num = 1;
while(true){
int cur = children.front();//cur是队首孩子的编号
children.pop();//喊完了,对手出队
if(num == m){
num = 1;
if(children.empty()){
printf("%d\n", cur);
break;
}else{
printf("%d\n", cur);
}
} else{
num = num + 1;
children.push(cur);
}
}
}
全部评论
相关推荐
2025-12-15 11:27
门头沟学院 Java 点赞 评论 收藏
分享
游客任冲丶:只能说世界很魔幻,一堆毕业即失业的,还有一堆嫌弃几十万package太少的 点赞 评论 收藏
分享
查看19道真题和解析
SHEIN希音公司福利 363人发布