解题思路: 通过list链表,动态删除报到3的元素
注意事项: 记得说明如果指针指向end,就换到begin位
参考代码:
#include<iostream>
#include<list>
using namespace std;
int main()
{
int num;
cin>>num;
int n=3;
list <int> people;
// 给人编号
for(int i=1;i<=num;i++)
people.push_back(i);
auto p = people.begin();
// 开始报数
while(people.size()!=1)
{
for(int i=1; i<n; i++)
{
++p;
if(p == people.end())
p = people.begin();
}
if(people.size()!=1)
p = people.erase(p);
if(p == people.end())
p = people.begin();
}
cout<<*p<<endl;;
return 0;
}
0.0分
0 人评分