图片来源于王道机试课程
代码
#include <queue>
#include <cstdio>
using namespace std;
int main() {
int n,p,m;
//queue<int> children;//队列中的元素是孩子的编号
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;// p->p+1->p+2->...->n->1->...->p-1
if (i > n){
i = 1;
}
}
//喊号的过程
int num = 1;//将要喊的编号
while(true){
int cur = children.front();//cur是队首孩子的编号
children.pop();//喊完了,队首出队
if(num == m){//检查一下刚才喊的号码是不是m
num = 1;//下一个同学喊的号就是1
//喊号的同学也不需要归队了
if (children.empty()){
// 最后一个同学出来了
printf("%d\n",cur);
break;
} else{
// 还有同学在喊号
printf("%d,", cur);
}
} else{
// 喊的号码不是m, 归队
num = num + 1;
children.push(cur);
}
}
}
}