题目:0.1....n-1这n个数字排成一个圆圈 从数字0开始每次从这个圆圈里删除第m个数字 求出这个圆圈里剩下的最后一个数字
int lastnum( int n, int m)
{
if (n < 1 || m < 1)
return false;
list<int > Listnum;
for (int i = 0;i < n;i++)
Listnum.push_back(i);
list<int>::iterator current = Listnum.begin();
while (Listnum.size()>1)
{
for (int i = 1;i < m;i++)
{
current++;
if (current == Listnum.end())
current = Listnum.begin();
}
list<int>::iterator next;
next = ++current;
if (next == Listnum.end())
next = Listnum.begin();
Listnum.erase(--current);
current = next;
}
return Listnum.front();
}