永发信息网

诚觅高手代写c++约瑟夫环代码,要求用类写!~~

答案:2  悬赏:20  手机版
解决时间 2021-07-30 00:10
  • 提问者网友:ミ烙印ゝ
  • 2021-07-29 02:54
要求用类,高手给个代码,在dev上编译通过就行!!
最佳答案
  • 五星知识达人网友:酒者煙囻
  • 2021-07-29 04:31
#include <iostream>
using namespace std;

class JosephusRing {
struct person {
int id;
person* next;
} *head, *prev, *next;
int n, m;
public:
JosephusRing( int nn, int mm ) {
n = nn; m = mm;
head = new person;
head->id = 1;
head->next = 0;
person *n = head;
for ( int i = 2; i <= nn; ++i ) {
n->next = new person;
n = n->next;
n->id = i;
n->next = 0;
}
n->next = head;
prev = n;
next = head;
}
void gameStart()
{
int i = 0;
person* tmp;
while( prev != next ) {
++i;
if ( i % m == 0 ) {
tmp = next;
prev->next = next->next;
next = next->next;
cout << "编号为" << tmp->id << "的人被T出" << endl;
delete tmp;
i = 0;
} else {
prev = prev->next;
next = next->next;
}
}
cout << "最后的幸存者是" << next->id << endl;
delete next;
}
};

int main()
{
JosephusRing r( 10, 2 );
r.gameStart();
}

全部回答
  • 1楼网友:骨子里都是戏
  • 2021-07-29 05:03

#include<iostream>

using namespace std;

class ysf

{

private:

int n,c=0,pos=2,*base;//从N开始数,则把pos改为N-1就行了.

public:

ysf()

{

cout<<"请输入总人数"<<endl; cin>>n;

int *base=new int[n];

for(int i=0;i<n;i++) base[i]=1;

}

~ysf()

{

delete[]base;

}

//本程序以从1开始数,数到3离队为例,其它情况只用少加修改 void make( ) { int j=0; cout<<"NO. "<<++c<<" 第"<<pos+1<<"位出列"<<endl;//输出 base[pos]=0;//踢掉 if(c==n)return; //出口 while(j-3) if(base[pos=(pos+1)%n]) j++;//递归点 ,每次数到几这个3就改到几 make( );//递归 }

};

int main()

{

ysf a;

a.make();

return 0;

}

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯