https://www.dotcpp.com/oj/problem1047.html
这一题没有好的思路,看了大神的题解,用队列简直不要太方便!!!几乎就是为这种题而设计的!!!
代码:
#include
#include
#include
#include
using namespace std;
int main()
{
int tot, outNum, nowNum = 1;
queue<int> q;
cin >> tot; //读取数据
for (int i = 1; i <= tot; i++)q.push(i); //初始化队列
while (!q.empty()) //在队列不为空时继续模拟
{
if (nowNum == 3)
{
if(q.size()==1)
cout << q.front() << " "; //打印最后一个人的编号
q.pop(); //出局
nowNum = 1; //初始化现在的数字
}
else
{
nowNum++;
q.push(q.front()); //将当前元素排至队尾
q.pop(); //释放当前元素
}
}
return 0;
}