josephus问题(基本对象的实现方法)

//------------------------------------------------------------------------
//  boyring.h
//  [2007-5-5 MyDragon]
//-------------------------------------------------------------------------
#ifndef HEADER_BOYRING
#define HEADER_BOYRING
//-------------------------------------------------------------------------
struct boy
{
 int Code;
 boy* Next;
};
//-------------------------------------------------------------------------
class BoyRing
{
 boy* pBegin;
 boy* piovt;
 boy* pCurent;
public:
 BoyRing(int n);
 int GetCode() const;
 void PrintAll() const;
 void CountBy(int m);
 void Disengage();
 ~BoyRing();
};
#endif 

//======================================================================
//  boyring.cpp
//  [2007-5-5 MyDragon]
//======================================================================
#include "boyring.h"
#include <iostream>
using namespace std;
//-------------------------------------------------------------------------
BoyRing::BoyRing(int n)
{
 if (n<2) throw exception();
 pBegin = new boy[n];
 for(int i=1; i<=n; i++)
 {
  pBegin[i-1].Code = i;
  pBegin[i-1].Next = &pBegin[i%n];
 }
    piovt = pCurent = &pBegin[n-1];
}
//-------------------------------------------------------------------------
int BoyRing::GetCode() const
{
 return pCurent->Code;
}

void BoyRing::Disengage()
{
 piovt->Next = pCurent->Next;
 pCurent = piovt;
}

void BoyRing::CountBy(int m)
{
 for(int i=1; i<=m; i++)
 {
  piovt = pCurent;
  pCurent = pCurent->Next;
 }
}

void BoyRing::PrintAll() const
{
 static int numline = 0;
 boy* p = pCurent;
    do
    {
  cout<<" "<<p->Code;
  if (!(++numline%10)) cout<<"/n";
        p = p->Next;
    } while(p!=pCurent);
}

BoyRing::~BoyRing()
{
 delete[] pBegin;
}
//=====================================================================

#ifndef HEADER_JOSE
#define HEADER_JOSE
//-------------------------------------------------------------------------
#include "boyring.h"
#include <iostream>
using namespace std;
//-------------------------------------------------------------------------
class Jose
{
 int n, m, s;
public:
 Jose(int boys, int interval, int start=1);
 void GetWinner() const;
};
//-------------------------------------------------------------------------
#endif

//=================================================

//-------------------------------------------------------------------------
//  Jose.cpp
//  [2007-5-5 MyDragon]
//-------------------------------------------------------------------------
#include "boyring.h"
#include "jose.h"
#include <iostream>
using namespace std;
//-------------------------------------------------------------------------

Jose::Jose(int boys, int interval, int start):n(boys), m(interval), s(start)
{
    if (n<2 || m<1 || m>=n || s<1 || s>=n)
    {
  cerr<<"data error./n";
     throw exception();
    }
}

void Jose::GetWinner() const
{
 cout<<"The are "<<n<<" boys./nBoys leaved in order:/n";
 BoyRing x(n);
 x.CountBy(s-1);
    for (int i=1, numlime=0; i<n-1; i++)
    {
  x.CountBy(m);
  cout<<x.GetCode()<<(++numlime%10 ? " ":"/n");
  x.Disengage();
    }
 cout<<"/nThe winner is /n"<<x.GetCode()<<"/n";
}

//==================================================

#include "jose.h"
#include <iostream>
using namespace std;

int main()
{
    cout<<"input boynum/internum/startpos/n";
 int n, m, s;
 cin>>n>>m>>s;
 Jose(n, m).GetWinner();
 Jose(n, m, s).GetWinner();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值