#include<iostream>
using namespace std;
struct node{
int n;
struct node *next;
};
node*makelist(int a)
{
node*p=new node;
if(p==NULL) return NULL;
p->n=1;
node*h=p;
node*q;
for(int i=2;i<=a;i++)
{ q=new node;
if(q==NULL) return NULL;
q->n=i;
h->next=q;
h=q;
}
h->next=p;
return p;
}
int main()
{
int num,m;
cout<<"input the num of kids:"<<endl;
cin>>num;
node*p=makelist(num);
cout<<"input the turn num:"<<endl;
cin>>m;
while(p->next!=p)
{ node*q=p;
if(m==1){while(q->next!=p)
q=q->next;
q->next=q->next->next;
p=q->next;
}
else
{
for(int i=0;i<m-2;i++)
{q=q->next;}
q->next=q->next->next;
p=q->next;
}
}
cout<<"the winner is:"<<p->n;
cin>>num;
}
[此贴子已经被作者于2007-11-6 16:43:02编辑过]