挺简单的一个问题
n个人坐成一圈,编号1-n,从编号为k的人开始报数,数到m的那个人清理出去,从下一个人继续从1开始报数。。如此往复
可知最终所有人都会出圈,求出圈次序
用循环链表做就很直接,也很好理解
#include <iostream>
using namespace std;
struct Listnode// basic structure of list
{
int index;
Listnode *next;
Listnode(int n=0,Listnode *p=NULL)
{
index=n;
next=p;
}
};
int n,m,k;
int main()
{
cin>>n>>m;
cin>>k;
Listnode *first=new Listnode();//head
Listnode *pre=NULL;// the point before p
Listnode *p=first;// work point,gets the main operation.
//initialization
int cnt=1;
p=first;
p->index=1;
//create the list
for(int i=2;i<=n;i++)
{
p->next=new Listnode(i);
p=p->next;
}
p->next=first;
pre=p;
p=first;
int tmp=k-1;
// jump to the s