#include <iostream>
using namespace std;
struct node
{
int date;
node *next;
};
class josephring
{
public:
josephring(int n);
~josephring(){};
void print(int m,int k);
private:
node * rear;
};
josephring::josephring(int n)
{
node *s=NULL;
rear =new node;
rear->date=1;rear->next=rear;
for(int i=2;i<=n;i++)
{
s=new node;
s->date= i;
s->next=rear->next;
rear->next=s;
rear=s;
}
}
void josephring::print(int m,int k)
{
node *pre=rear,*p=rear->next;
int count=1;
while(p!=NULL&&count<k)
{
pre=pre->next;
p=p->next;
count++;
}
count=1;
while(p->next!=p)
{
if(count<m)
{
pre=p;p=p->next;count++;
}
else
{
pre->next=p->next;
delete p;
p=pre->next;
count=1;
}
}
cout<<p->date<<"\t";
delete p;
}
int main()
{
int n,m,k;
cin>>n;
cin>>k;
cin>>m;
josephring r(n);
r.print(m,k);
return 0;
}
约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律
最新推荐文章于 2021-11-19 20:36:33 发布