题目链接:报数
题目思路:是一个循环队列的问题;挨个报数,不到第m个就先存下来,从队列头pop出去,然后push到尾部;
如果报到第m个数,就直接pop出去,直到队列只剩1个,即。size()==1
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, m, i;
queue<int>a; //循环队列,用于存id
scanf("%d%d", &n, &m);
for(i = 1; i <= n; i++)
a.push(i);
int s = 0;
while(a.size()>1) //只剩一个id退出循环
{
s++;
int b = a.front(); //记录下来队列头部的数字,以便else的操作
if(s==m)
{
a.pop();
s = 0;
}
else
{
a.pop();
a.push(b);
}
}
printf("%d\n",a.front());
return 0;
}