【c++提问】约瑟夫环问题

My_Epoch My_Epoch 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
/***
约瑟夫环
编号为1,2,3,……,n的n个人按顺时针方向围坐一圈。
任选一个正整数作为报数上限m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止报数。
报m的人出列,从他在顺时针方向上的下一个人开始重新从1报数,如此下去,直至所有人全部出列为止。
设计程序输出出列顺序。
***/
/**
输入
人数n 报数上限m
人员记录1 (格式为:姓名 学号 性别 年龄 班级 健康状况)
人员记录2
…
人员记录n
输出
第1次报数出列的人员记录
第2次报数出列的人员记录
…
第n次报数出列的人员记录
**/
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string>
using namespace std;

struct StudentNode
{
string Sname;
long Sno;
string Ssex;
int Sage;
string Sclass;
string Shealth;
StudentNode* next;

StudentNode(const string& StudentName, const long& StudentNo, const string& StudentSex,
const int& StudentAge, const string& StudentClass, const string& StudentHealth,
StudentNode* ptr=NULL)//用于初始化
{
Sname = StudentName;
Sno = StudentNo;
Ssex = StudentSex;
Sage = StudentAge;
Sclass = StudentClass;
Shealth = StudentHealth;

next = NULL;
}
};

class StudentList
{
private:
StudentNode* first;//头指针
public:
StudentList()
{
first = NULL;
}
StudentList(const StudentNode& L)
{
first = NULL;
CopyStudentList(L);
}
StudentList operator=(const StudentList& L)
{
if(this == &L)
return *this;
MakeEmpty();
CopyStudentList(L);
return *this;
}
~StudentList()
{
//头删法
StudentNode* p;
while(p)
{
p=first;
first = p->next;
//last = p->next;
delete p;
}
}

void InputFront(const string& StuName, const long& StuNo, const string& StuSex,
const int& StuAge, const string& StuClass, const string& StuHealth);
void InputRear(const string& StuName, const long& StuNo, const string& StuSex,
const int& StuAge, const string& StuClass, const string& StuHealth);
bool Insert(const int i, const string& StuName, const long& StuNo, const string& StuSex,
const int& StuAge, const string& StuClass, const string& StuHealth);
bool Search(const string& StuName, const long& StuNo, const string& StuSex,
const int& StuAge, const string& StuClass, const string& StuHealth);
bool Remove(int i, string& StuName, long& StuNo, string& StuSex,
int& StuAge, string& StuClass, string& StuHealth);

void CopyStudentList(const StudentList& L);
void MakeEmpty();

StudentNode* LocateRear()
{
StudentNode* iter = first;
do
{
iter = iter->next;
}
while(iter->next != first);
return iter;
}

int Length() const
{
StudentNode* s=first;
int count=1;
while(s->next==first)
{
count++;
s = s->next;
}
return count;
}
bool IsEmpty() const
{
return first==NULL;
}
bool IsFull() const
{
return false;
}

void Output(const int num, const int count)
{
StudentNode* pre = first;
StudentNode* p = NULL;
int n = 0;

for(int i = 0; i < num; i++)
{
if(n == num-1)
{
cout << pre->Sname << " " << pre->Sno << " "
<< pre->Ssex << " " << pre->Sage << " "
<< pre->Sclass << " " << pre->Shealth;
}
else
{
for(int j = 1; j<count; j++)
{
p = pre;
pre = pre->next;
//a = i;
}
cout << pre->Sname << " " << pre->Sno << " "
<< pre->Ssex << " " << pre->Sage << " "
<< pre->Sclass << " " << pre->Shealth << endl;
p->next = pre->next;
delete pre;
pre = p->next;
n++;
}
}
pre = NULL;
p = NULL;
return;
}
friend ostream& operator<<(ostream& out, const StudentList& Stu)
{
StudentNode* iter = Stu.first;
while(iter)
{
out << iter->Sname << " " << iter->Sno << " "
<< iter->Ssex << " " << iter->Sage << " "
<< iter->Sclass << " " << iter->Shealth << endl;
iter = iter->next;
}
return out;
}
friend istream& operator>>(istream& in, StudentList& L)
{
return in;
}
};

void StudentList::CopyStudentList(const StudentList& L)
{
if(L.first==NULL)
{
MakeEmpty();
}
else
{
StudentNode* iter = first;
do
{
InputRear(iter->Sname, iter->Sno, iter->Ssex,
iter->Sage, iter->Sclass, iter->Shealth);
iter = iter->next;
}
while(iter==first);
}
return;
}
void StudentList::MakeEmpty()
{
StudentNode* del;
while(first)
{
//尾删法
StudentNode* item = first;
del = item->next;
while(del->next==first)
{
item = item->next;
del = del->next;
}
item->next = first;
delete del;
}
return;
}
void StudentList::InputFront(const string& StuName, const long& StuNo, const string& StuSex,
const int& StuAge, const string& StuClass, const string& StuHealth)
{
StudentNode* newNode = new StudentNode(StuName, StuNo, StuSex, StuAge, StuClass, StuHealth);
newNode->next = first;
first = newNode;
StudentNode* pre = LocateRear();
pre->next = first;
}
void StudentList::InputRear(const string& StuName, const long& StuNo, const string& StuSex,
const int& StuAge, const string& StuClass, const string& StuHealth)
{
StudentNode* newNode = new StudentNode(StuName, StuNo, StuSex, StuAge, StuClass, StuHealth);
if(first == NULL)
{
first = newNode;
}
else
{
StudentNode* iter = first;
do
{
iter = iter->next;
}
while(iter->next != first);
iter->next = newNode;
newNode->next = first;
}

}
bool StudentList::Insert(int i,const string& StuName, const long& StuNo, const string& StuSex,
const int& StuAge, const string& StuClass, const string& StuHealth)
{
StudentNode* newNode = new StudentNode(StuName, StuNo, StuSex, StuAge, StuClass, StuHealth);
if(i < 1)
{
cout << "Location error" << endl;
return false;
}
if(i == 1)
{
first = newNode;
newNode->next = first;
return true;
}
else
{
StudentNode* pre = first;
for(int j=1; j<i-1; j++)
{
pre = pre->next;
}
newNode->next = pre->next;
pre->next = newNode;
return true;
}
}
bool StudentList::Remove(int i, string& StuName, long& StuNo, string& StuSex,
int& StuAge, string& StuClass, string& StuHealth)
{
StudentNode* del;
StudentNode* pre;
if(IsEmpty())
{
cout << "Empty" << endl;
return false;
}
if(i < 1)
{
cout << "Location error" << endl;
return false;
}
if(i == 1)
{
del = first;
first = del->next;
StuName = del->Sname;
StuNo = del->Sno;
StuSex = del->Ssex;
StuAge = del->Sage;
StuClass = del->Sclass;
StuHealth = del->Shealth;

StudentNode* iter = LocateRear();
iter->next = del->next;
delete del;
return true;
}
else
{
pre = first;
if(pre->next == NULL)
{
cout << "Location error" << endl;
return false;
}
pre = pre->next;
for(int j = 1; j < i-2; j++)
{
if(pre->next == first)
{
cout << "Location error" << endl;
return false;
}
pre = pre->next;
}
del = pre->next;
pre->next = del->next;

StuName = del->Sname;
StuNo = del->Sno;
StuSex = del->Ssex;
StuAge = del->Sage;
StuClass = del->Sclass;
StuHealth = del->Shealth;

delete del;
return true;
}
}

int main()
{
StudentList Stu;
int n, m;//n为人数,m为报数上限
cin >> n >> m;
for(int i = 0; i < n; i++)
{
string Sname;
long Sno;
string Ssex;
int Sage;
string Sclass;
string Shealth;
cin >> Sname >> Sno >> Ssex >> Sage >> Sclass >> Shealth;
Stu.Insert(i+1, Sname, Sno, Ssex, Sage, Sclass, Shealth);
}
Stu.Output(n, m);

return 0;
}



程序输出正常,但是不能正常结束。程序跳出Output函数开始执行return 0语句时会报错……不知道什么原因qwq求解。
...全文

相关推荐

约瑟夫环问题

设计性实验 约瑟夫环问题。在校大学生,课程设计的文档。

yedfl

C++实现约瑟夫环问题代码详解

约瑟夫环问题,又称为约瑟夫斯问题(Josephus Problem),是计算机科学和数学领域中一个著名的理论问题。该问题源于一个历史传说:古代犹太历史学家约瑟夫与同伴被困于一个洞穴中,他们决定通过一个残酷的规则来确定谁将与敌人战斗至死,以保留其他人生还的机会。规则如下:他们围成一个圈,从某个人开始计数,每数到第三个人,该人就必须出列,接着从下一个人开始继续计数,直到只剩下一人。虽然这个问题听起来充满了残酷与悲伤,但它实际上是一个在计算机算法与数学上具有深远意义的模型。

苏西苏西

约瑟夫环问题用C++代码实现

8. 【题目】约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为1的人开始报数,数到k的那个人出列;他的下一个人又从1开始报数,数到k的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。 要求:(1)定义递归函数int jos(int n, int k); n表示总人数, k表示报数的第几个数,返回最后一个人的编号。 (2)在主函数中输入总人数和报的数,输出最后一个人的编号。

一只宅

基于C++约瑟夫环问题

本质上是一个约瑟夫环,以杀人的形式实现,输出杀人过程及最后一个存活的人,C++指针的应用

henryhoo497

c++链表,解决约瑟夫环问题

自己写的链表,并用链表解决了约瑟夫环问题,大家给个意见,主要是链表

beefliu
评论 4
收藏 已收藏
分享
返回顶部
My_Epoch
C++ 语言 65,214 成员 250,514 内容