哪位高手帮小弟看看错在哪了?新手
用链表实现报数 m个人报数报到n的人退出 最后剩下那个人#include<iostream.h>
struct rec
{ int number;
rec *next;
};
rec *creat(int i)
{rec *head;
rec *p1,*p2;
p1=p2=new rec;
int n=0;
int k=0;
cout<<"the number"<<endl;
cin>>p1->number;
head=NULL;
while(k<i-1)
{n++;
k++;
if(n==0)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new rec;
cout<<"the number "<<endl;
cin>>p1->number;
}
p2->next=NULL;
return (head);
}
main()
{int j,n,k,m,i;
cout<<"the nuber you will stop:"<<endl;
cin>>n;
cout<<"how many people there are?"<<endl;
cin>>i;
rec *head,*p1,*p2;
if(n>i)
cout<<"error!"<<endl;
else
{k=0;
m=0;
j=0;
head=creat(i);
/*do
{cout<<head->number<<" ";
head=head->next;
}while(head!=NULL);*/
p1=p2=head;
while(m<i)
{ if(p1->number!=0)
k++;
if(k==n)
{ p1->number=0;
k=0;
m++;
}
p2->next=p1;
p2=p1;
j++;
if(j==i)
j=0;
}
p1=p2=head;
while(p1->number==0)
{p2->next=p1;
p2=p1;
}
cout<<"the last one is:"<<p1->number<<endl;
}
}