第2个回答 2011-04-27
//没仔细想,就是简单的模拟了一下,貌似复杂度有点高
#include <iostream>
#include <list>
#define MAX_VALUE 100
#define STEP 10
using namespace std;
int main()
{
list<int> l;
for(int i=1;i<=MAX_VALUE;i++)
l.push_back(i);
list<int>::iterator it = l.begin();
int count = 0;
while(l.size() > 1)
{
if(it == l.end())
it = l.begin();
if(count == STEP)
{
count = 1;
list<int>::iterator tmp = it++;
l.erase(tmp);
continue;
}
it++;
count++;
}
cout<<"left number is "<<l.front()<<endl;
return 0;
}
第3个回答 2011-04-27
#include <iostream>
#include <list>
#define MAX_VALUE 100
#define STEP 10
using namespace std;
int main()
{
list<int> l;
for(int i=1;i<=MAX_VALUE;i++)
l.push_back(i);
list<int>::iterator it = l.begin();
int count = 0;
while(l.size() > 1)
{
if(it == l.end())
it = l.begin();
if(count == STEP)
{
count = 1;
list<int>::iterator tmp = it++;
l.erase(tmp);
continue;
}
it++;
count++;
}
cout<<"left number is "<<l.front()<<endl;
return 0;
}