//所谓约瑟夫(Josephus)问题指的是假设有n个人围坐一圈,先由某个位置start的人站起来,并
//从后一个人开始报数,数到m的人就要站出来,然后从这个人的下一个人重新开始报数,再数到
//从后一个人开始报数,数到m的人就要站出来,然后从这个人的下一个人重新开始报数,再数到
//m的人站出来,一次重复下去,知道所有的人都站出来为止,则站出来的人的次序如何?
方法一:
#include<iostream>
using namespace std;
void Josephus(int n,int start,int m);//n人数,start开始位置
int main()
{
int n,start,m;
cin>>n;
cin>>start;
cin>>m;
Josephus(n,start,m);
cout<<endl;
return 0;
}
void Josephus(int n,int start,int m)
{
int *Array=new int [n];
int i,count=n;
for(i=0;i<n;i++)
Array[i]=i+1;
start--;//由于数组从0下标开始,方便输出,所以start-1
while(count>1)//被除数不能为0
{
cout<<Array[start]<<" ";
for(i=start;i<count-1;i++)
Array[i]=Array[i+1];
start=(start-1+m)%(--count);//因为位置变了座椅start-1
}
cout<<Array[start]<<endl;
}
方法二:
CircList.h
template<class T>
struct CircLinkNode{
T data;
CircLinkNode *link;
CircLinkNode(CircLinkNode<T>*next=0)
{link=next;}
CircLinkNode(T x,CircLinkNode<T>*next=0)
{data=x;link=next;}
bool operator ==(T x)
{return data==x?true:false;}
bool operator !=(T x)
{return data!=x?true:false;}
};
template<class T>
class CircList{
private:
CircLinkNode<T>*first,*tail;
public:
CircList();
CircList(const T x);
CircList(CircList<T>&L){first=L.first;tail=L.tail;}
~CircList();
int Length()const;
bool IsEmpty()
{return first->link==first?true:false;}
CircList<T>*getHead()const;
void setHead(CircList<T>*p);
CircLinkNode<T>*Search(T x);
CircLinkNode<T>*Locate(int i);
T *getData(int i);
bool Insert(int i,T x);
bool Remove(int i,T&x);
void Create(int n);
void Output();
void Josephus(int n,int start,int m);
};
template<class T>
CircList<T>::CircList()
{
first=tail=new CircLinkNode<T>;
first->link=first;
}
template<class T>
CircList<T>::CircList(const T x)
{
first=new CircLinkNode<T>;
CircLinkNode<T>*p=new CircLinkNode<T>(x);
first->link=p;
p->link=first;
tail=p;
}
template<class T>
CircList<T>::~CircList()
{
CircLinkNode<T>*p;
while(first->link!=first)
{
p=first->link;
first->link=p->link;
delete p;
}
delete first;
}
template<class T>
CircLinkNode<T>*CircList<T>::Search(T x)
{
CircLinkNode<T>*current=first->link;
while(current->link!=first&¤t!=x)
current=current->link;
if(current!=first)
return current;
return 0;
}
template<class T>
void CircList<T>::Create(int n)
{
int data=0;
delete first;
CircLinkNode<T>*current=first=tail=new CircLinkNode<T>;
for(int i=0;i<n;i++)
{
CircLinkNode<T>*p=new CircLinkNode<T>;
p->data=++data;
p->link=first;
current->link=p;
current=p;
tail=p;
}
}
template<class T>
void CircList<T>::Output()
{
CircLinkNode<T>*current=first->link;
while(current!=first)
{
cout<<current->data<<" ";
current=current->link;
}
}
template<class T>
void CircList<T>::Josephus(int n,int start,int m)
{
Create(n);
int i;
if(IsEmpty())return;
first=first->link;
tail->link=first;//构成没有表头的循环链
for(i=1;i<start;i++)
{
first=first->link;
tail=tail->link;
}
while(!IsEmpty())
{
cout<<first->data<<" ";
tail->link=tail->link->link;
delete first;
first=tail->link;
for(i=1;i<m;i++)
{
first=first->link;
tail=tail->link;
}
}
cout<<first->data<<endl;
}
#include"CircList.h"
#include<iostream>
using namespace std;
void Josephus(int n,int start,int m);
int main()
{
CircList<int>cl;
int number,start,m;
cout<<"input the number:";
cin>>number;
cout<<"input the start:";
cin>>start;
cout<<"input the m:";
cin>>m;
cl.Josephus(number,start,m);
return 0;
}