约瑟夫问题求解

约瑟夫问题求解:

版本一://数组实现方式,扩展名为.cpp 

Code:
  1. #include<stdio.h>   
  2. #include<assert.h>   
  3.   
  4. int *Joseph(int n){   
  5.  int *p;   
  6.  p=new int[n];   
  7.  for(int i=0;i<n;i++)   
  8.   p[i]=i+1;   
  9.  return p;   
  10. }   
  11.   
  12. int NextPeople(int pre,int *WorkingArray,int number_of_people,int step){   
  13.  //计算下一个出列的人的序号   
  14.  int count=0;   
  15.  int travel=pre;     
  16.  int emptyplace=0;   
  17.  while(count<step){   
  18.   travel=(travel+1)%number_of_people;   
  19.   if(WorkingArray[travel]){   
  20.    count++;   
  21.    emptyplace=0;   
  22.   }   
  23.   else {   
  24.    emptyplace++;   
  25.    if(emptyplace==number_of_people)   
  26.     return -1;   
  27.   }   
  28.  }   
  29.  return travel; //返回出列人的序号   
  30. }   
  31.   
  32. void CreatOutput(int *WorkingArray,int number_of_people,int step){   
  33.  //输出所有人的出列序号   
  34.  int i;   
  35.  int outnum=-1;   
  36.  for(i=0;i<number_of_people;i++){   
  37.   outnum=NextPeople(outnum,WorkingArray,number_of_people,step);   
  38.   assert(outnum>-1 && outnum<number_of_people);  //断言函数   
  39.   printf("%d ",WorkingArray[outnum]);   
  40.   WorkingArray[outnum]=0;   
  41.  }   
  42.  printf("/n");   
  43. }   
  44.   
  45. void Destory(int *WorkingArray){   
  46.  //销毁申请的数组空间   
  47.  delete[]WorkingArray;   
  48. }   
  49. //测试   
  50. int main(){   
  51.  int n,m;   
  52.  int *WorkingArray;   
  53.  printf("Input number of people:");   
  54.  scanf("%d",&n);   
  55.  printf("Input step:");   
  56.  scanf("%d",&m);   
  57.  WorkingArray=Joseph(n);   
  58.  CreatOutput(WorkingArray,n,m);   
  59.  Destory(WorkingArray);   
  60.   
  61.  return 0;   
  62. }   
  63.   

版本二://C++版,数组实现

Code:
  1. #include<iostream>   
  2. #include<cassert>   
  3. using namespace std;   
  4. class Joseph{   
  5. protected:   
  6.  int number_of_people;   
  7.  int step;   
  8. public:   
  9.  virtual void CreateOutput()=0;   
  10.  Joseph(int n,int m){   
  11.   number_of_people = n;   
  12.   step = m;   
  13.  }   
  14. };   
  15. class JoseArray:public Joseph{   
  16.  int *WorkingArray;   
  17.  int NextPeople(int pre);   
  18. public:   
  19.  JoseArray(int n,int m);   
  20.  void CreateOutput();   
  21.  ~JoseArray(){delete[]WorkingArray;}   
  22. };   
  23. JoseArray::JoseArray(int n,int m):Joseph(n,m){   
  24.  WorkingArray=new int[n];   
  25.  for(int i=0;i<n;i++)   
  26.   WorkingArray[i]=i+1;   
  27. }   
  28. void JoseArray::CreateOutput(){   
  29.  int i;   
  30.  int outnum=-1;   
  31.  for(i=0;i<number_of_people;i++){   
  32.   outnum=NextPeople(outnum);   
  33.   assert(outnum>-1&&outnum<number_of_people);   
  34.   cout<<WorkingArray[outnum]<<" ";   
  35.   WorkingArray[outnum]=0;   
  36.  }   
  37.  cout<<endl;   
  38. }   
  39.   
  40. int JoseArray::NextPeople(int pre){   
  41.  //计算下一个出列的人的序号   
  42.  int count=0;   
  43.  int travel=pre;     
  44.  int emptyplace=0;   
  45.  while(count<step){   
  46.   travel=(travel+1)%number_of_people;   
  47.   if(WorkingArray[travel]){   
  48.    count++;   
  49.    emptyplace=0;   
  50.   }   
  51.   else {   
  52.    emptyplace++;   
  53.    if(emptyplace==number_of_people)   
  54.     return -1;   
  55.   }   
  56.  }   
  57.  return travel; //返回出列人的序号   
  58. }   
  59.   
  60. int main(){   
  61.  int n,m;   
  62.  cout<<"Input number of people:";   
  63.   cin>>n;   
  64.  cout<<"Input step:";   
  65.   cin>>m;   
  66.  JoseArray obj(n,m);   
  67.  obj.CreateOutput();   
  68.   
  69.  return 0;   
  70. }   

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值