王道约瑟夫问题

#include
#include
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 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);
}

}
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务