n 个小孩围成一圈做游戏,游戏将决出一个胜利者。假定一个数 m ,从第 s 个小孩起顺时针计数,每数到第 m 个小孩时该小孩离开;接着又从下一个小孩开始数数,数到第 m 个小孩时该小孩也离开;如此不断反复进行,最后剩下的一个小孩便是胜利者。
对于一定的n、m 和 s,究竟谁是胜利者呢?
链表:
#include <iostream>
#include <iomanip>
using namespace std;
struct Jose //小孩结点
{
int code;//小孩编号
Jose* pNext;//指向下一个小孩结点
};
int main()
{
//赋初值
int nBoyNum,nInterval;
int i,j;
cout <<"please input the number of boys,\n" //小孩数
<<"interval of counting:\n"; //数小孩个数
cin >>nBoyNum >>nInterval;
Jose *pHead,*pNew,*pTmp;
pHead = NULL;
for (i=0; i<nBoyNum; i++)
{
//创建新节点
pNew = new Jose;
if (pNew==NULL)
{
cout << "Can not new Jose!";
return -1;
}
pNew->code = i+1;
pNew->pNext = NULL;
if (pHead == NULL)
pHead = pNew;//插入到链表的头部
else
{
//找到尾结点
pTmp = pHead;
while (pTmp->pNext != NULL)
pTmp = pTmp->pNext;
pTmp->pNext = pNew;
}
}
pNew->pNext = pHead; // 构成循环链表
cout<<"The all Boys:";
pTmp = pHead;
while(pTmp->pNext != pHead)
{
cout<<pTmp->code<<",";
pTmp = pTmp->pNext;
}
cout<<pTmp->code<<endl;
Jose *pPrev,*pCur;
pCur = pPrev = pHead;
cout << "The leaving Boy:";
while (pCur->pNext != pCur)
{
for(j=0; j<nInterval-1; j++)
{
pPrev = pCur;
pCur = pCur->pNext;
}
// 删除
cout << pCur->code<<",";
pHead = pCur->pNext;
pPrev->pNext = pHead;
delete pCur;
pCur = pHead;
}
cout << "\nThe winner is "<<pCur->code<<endl;
return 0;
}
// josephus problem procedural solving
//当s=1的时候,i=1,i不小于s,所以不符合;
#include<iostream>
using namespace std;
//-------------------------------------
struct Jose // 小孩结点
{
int code; // 小孩编号
Jose* pNext; // 指向下一个小孩结点
};//-----------------------------------
//-------------------------------------
bool GetValue(int &n,int &s, int &m);
Jose* CreateRing(Jose* pHead, int n, int s); // 创建循环链表
Jose* CountBoys(Jose *pHead, int m); // 数m个小孩
Jose* Process(Jose *pHead, int m); // 排除n-1个小孩
//-------------------------------------
//-------------------------------------
int main()
{
int n, s, m;
Jose *pHead = NULL;
if (!GetValue(n,s,m))
return 0;
pHead = CreateRing(pHead, n, s);
pHead = Process(pHead,m);
cout<<"\nThe winner is "<<pHead->code<<"\n";
delete pHead;
return 1;
}
//------------------------------------
bool GetValue(int &n, int &s, int &m)
{
cout <<"please input boyNumber, startPosition, intervalNumber:\n";
cin>>n>>s>>m;
if (n>=2 && s>=1 && s<=n && m>=1 && m<=n)
return true;
cerr<<"failed in bad boyNumber or startPosition or intervalNumber.\n";
//发给它的内容立即输出;
return false;
/*else
{
cout<<"failed in bad boyNumber or startPosition or intervalNumber.\n";
return false;
}*/
}//------------------------------------
Jose* CreateRing(Jose *pHead, int n,int s)
{
Jose *pCur,*pPrev;
for(int i=1; i<=n; i++)
{
// 创建新节点
pCur = new Jose;
pCur->code = i;
pCur->pNext = NULL;
// 将新节点加入链表
if (pHead == NULL)
{
pHead = pCur;
pPrev = pCur;
}
else
{
pPrev->pNext = pCur;
pPrev = pCur;
}
}//------------------------
pCur->pNext = pHead; //构成循环链表
cout<<"There are "<<n<<" boys.\n";
pPrev = CountBoys(pHead,s);
pHead = pPrev->pNext;
return pHead;
}//------------------------------------
Jose * CountBoys(Jose* pHead, int m)
{
Jose *pCur,*pPrev;
if (pHead->pNext == pHead)
return pHead;
pCur = pHead;
for(int i=1; i<m; i++)
{
pPrev = pCur;
pCur = pCur->pNext;
}
return pPrev;
}//------------------------------------
Jose* Process(Jose *pHead, int m)
{
Jose *pCur,*pPrev;
cout<<"Boys leaved in order:\n";
while(pHead->pNext != pHead)
{
pPrev = CountBoys(pHead, m);
pCur = pPrev->pNext;
pHead = pCur->pNext;
static int line=0;
cout<<" "<<pCur->code;
if(!(++line%10))
cout<<endl;
pPrev->pNext = pHead; //小孩脱链
delete pCur;
}
return pHead;
}//====================================