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

My_Epoch 2017-10-14 08:31:07
/***
约瑟夫环
编号为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求解。
...全文
339 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-10-16
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
x_xx_xxx_xxxx 2017-10-16
  • 打赏
  • 举报
回复
运行了一下你的程序,也遇到了崩溃,觉得是指针的问题。没有充裕时间细调试。没有确定下来
大漠孤雁by 2017-10-16
  • 打赏
  • 举报
回复
#include<iostream> using namespace std; int main() { int i; //i用来记录数的那个数字 int n,m; int k,j; //k用来记录淘汰的人数 ,j用来表示第j个人 cout<<"请输入总人数 n和报数淘汰位 m:"; cin>>n>>m; int *Array=new int[n]; //用数组对n个人进行编号 for(i=0;i<n;i++) { Array[i]=i+1; } i=1; k=0; /*因为n个人围成一个圈,所以当j>n的时候,j要除n取余,例如(n+1)%n=1 之所以要++j,而不是j++是因为要先加1再判断*/ for(j=0;k<n-1;j=++j%n) { if(Array[j]!=0) //当j对应的人没有被淘汰时参与进来,淘汰的直接忽略 { if(i==m) //当j对应的人数到m的时候 { k++; //淘汰人数加1 Array[j]=0; //第K次淘汰的人(j+1)号对应数组的数据置为0 cout<<"第"<<k<<"次退出的是"<<j+1<<"号,\t "; i=1; //重新开始报数 } else { i++; //当j对应的人数没有数到m,则进行下一个 } } } for(i=0;i<n;i++) { if(Array[i]!=0) //通过上面的操作,所有淘汰的人员数组对应的值都为0,不为0的就是留下来的 { cout<<"留下来的是"<<i+1<<"号"<<endl; } } cout<<endl; system("pause") ; return 0; }
赵4老师 2017-10-15
  • 打赏
  • 举报
回复
百度搜相关关键字。

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧