#include <iostream>
#include <list>
#include <iterator>
using namespace std;
int main()
{
list<int> L;
L.push_back(5);
int account,people;
cin>>account;
while(account--)
{
cin>>people;
L.clear();
for(int i=0;i<people;i++)
{
L.push_back(i+1);
}
while(L.size()>3)
{
list<int>::iterator it=L.begin();
list<int>::iterator box=L.begin();
while(it!=L.end())
{
it++;
if(it==L.end())
{
break;
}
box=it;
it++;
L.erase(box);
}
if(L.size()<4)break;
it=L.begin();
while(it!=L.end())
{
it++;
if(it==L.end())break;
it++;
if(it==L.end())break;
box=it;
it++;
L.erase(box);
}
}
list<int>::iterator it=L.begin();
while(it!=L.end())
{
cout<<*it;
it++;
if(it!=L.end())cout<<' '; //空格别弄多了
}
cout<<endl;
}
return 0;
}
本文通过一个具体的C++程序示例,展示了如何使用STL中的list容器进行元素的添加、删除以及遍历操作,并实现了一种特殊的元素移除算法。该算法在列表中交替删除元素直到只剩三个元素,最后输出剩余的元素。
517

被折叠的 条评论
为什么被折叠?



