Description
n个人围坐一圈,从1到n编号,从1号开始报数(从1报至m),报m者退出;再从1报数,报m者退出。依退出顺序输出他们的编号。
Input
n m
Output
顺序退出之人编号。每个编号各占一行,前后不留空格。
Sample Input
10 3
Sample Output
3 6 9 2 7 1 8 5 10 4
KEY:用循环链表(其实很简单,不用也行)
Source:#include < iostream >
using namespace std;
struct node
... {
int num;
node *next;
} ;
int N;
int m;
void fun()
... {
node *head,*p,*q;
head=new(node);
head->num=1;
q=head;
int i;
for(i=2;i<=N;i++)
...{
p=new(node);
p->num=i;
q->next=p;
q=p;
}
p->next=head;
int n=N;
p=head;
while(n!=0)
...{
for(i=1;i<m-1;i++) p=p->next;
q=p->next;
cout<<q->num<<endl;
p->next=q->next;
p=p->next;
free(q);
n--;
}
}
int main()
... {
cin>>N;
cin>>m;
fun();
}