//------------------------------------------------------------------------
// 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;
}