josephus问题

 1 //=====================================
 2 // f1102.cpp
 3 // Josephus Problem Object-based Solving
 4 //=====================================
 5 #include"jose.h"
 6 #include<iostream>
 7 using namespace std;
 8 //-------------------------------------
 9 int main(){
10   cout<<"please input boyNum/interNum/startPos:\n";
11   int n, m, s;
12   cin>>n>>m>>s;
13   Jose(n,m).getWinner();
14   Jose(n,m,s).getWinner();
15   system("pause");
16 }//====================================
17 
18  
View Code
 1 //=====================================
 2 // jose.cpp
 3 //=====================================
 4 #include"boyring.h"
 5 #include"jose.h"
 6 #include<iostream>
 7 using namespace std;
 8 //-------------------------------------
 9 Jose::Jose(int boys, int interval, int begin, int wins)
10   :n(boys),m(interval),s(begin),w(wins){
11   if(n<2 || m<1 || m>=n || s<0 || s>=n || w<1 || w>=n){
12     cout<<"data error.\n";
13     throw exception();
14   }
15 }//------------------------------------
16 void Jose::getWinner()const{
17   cout<<"\nThere are "<<n<<" boys.\nBoys leaved in order:\n";
18   BoyRing x(n);
19   x.countBy(s-1);
20   for(int i=1,numinLine=0; i<n-w+1; ++i){
21     x.countBy(m);
22     cout<<"  "<<x.getNum()<<(++numinLine%10 ? "" : "\n");
23     x.disengage();
24   }
25   cout<<"\nwinners:\n";
26   x.printAll();
27 }//------------------------------------
28 
29  
View Code
 1 //=====================================
 2 // jose.h
 3 //=====================================
 4 #ifndef HEADER_JOSE
 5 #define HEADER_JOSE
 6 class Jose{
 7   int n, s, m, w;
 8 public:
 9   Jose(int boys, int interval, int begin=1, int wins=1);
10   void getWinner()const;
11 };//===================================
12 #endif  // HEADER_JOSE
13 
14  
View Code
 1 //=====================================
 2 // boyring.h
 3 //=====================================
 4 #ifndef HEADER_BOYRING
 5 #define HEADER_BOYRING
 6 struct Boy{
 7   int code;
 8   Boy* next;
 9 };//-----------------------------------
10 class BoyRing{
11   Boy *pBegin, *pivot, *pCurrent;
12 public:
13   BoyRing(int n);
14   void countBy(int m);
15   int getNum() const;
16   void disengage();
17   void printAll()const;
18  ~BoyRing();
19 };//===================================
20 #endif  // HEADER_BOYRING
21 
22  
View Code
 1 //=====================================
 2 // boyring.cpp
 3 //=====================================
 4 #include"boyring.h"
 5 #include<iostream>
 6 using namespace std;
 7 //-------------------------------------
 8 BoyRing::BoyRing(int n){
 9   if(n<2)
10       throw exception();//抛出异常,是库函数
11   pBegin = new Boy[n];
12   for(int i=1; i<=n; i++){
13     pBegin[i-1].next = &pBegin[i%n];
14     pBegin[i-1].code = i;
15   }
16   pivot = pCurrent = &pBegin[n-1];
17 }//------------------------------------
18 void BoyRing::countBy(int m){
19   for(int i=1; i<=m; ++i){
20     pivot = pCurrent;
21     pCurrent = pCurrent->next;
22   }
23 }//------------------------------------
24 int BoyRing::getNum() const {
25   return pCurrent->code;
26 }//------------------------------------
27 void BoyRing::disengage(){
28   pivot->next = pCurrent->next;
29   pCurrent = pivot;
30 }//------------------------------------
31 void BoyRing::printAll()const{
32   int numinLine = 0;
33   Boy* p = pCurrent;
34   do{
35     cout<<"  "<<p->code;
36     if(!(++numinLine%10)) 
37         cout<<"\n";
38     p = p->next;
39   }while(p!=pCurrent);
40   cout<<"\n";
41 }//------------------------------------
42 BoyRing::~BoyRing(){
43   delete[] pBegin;
44 }//------------------------------------
45 
46  
View Code

 

posted @ 2013-06-19 20:40  herizai  阅读(211)  评论(0编辑  收藏  举报