1 Star 1 Fork 0

小新ai/NWAFU-OJ题目试做

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
1400: 约瑟夫环.cpp 3.31 KB
一键复制 编辑 原始数据 按行查看 历史
小新ai 提交于 2022-05-14 06:54 . add 1400: 约瑟夫环.cpp.
/*
题目描述
约瑟夫环
编号为1,2,3,……,n的n个人按顺时针方向围坐一圈。任选一个正整数作为报数上限m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止报数。报m的人出列,从他在顺时针方向上的下一个人开始重新从1报数,如此下去,直至所有人全部出列为止。设计程序输出出列顺序。
输入
人数n 报数上限m
人员记录1 (格式为:姓名 学号 性别 年龄 班级 健康状况)
人员记录2
人员记录n
输出
第1次报数出列的人员记录
第2次报数出列的人员记录
第n次报数出列的人员记录
样例输入
5 3
安弥邵 10000001 女 28 计43 一般
宰觅 10000002 男 23 计79 健康
顾健 10000003 男 27 计29 一般
宓顽芳 10000004 女 20 计17 健康
能纸垄 10000005 男 18 计11 健康
样例输出
顾健 10000003 男 27 计29 一般
安弥邵 10000001 女 28 计43 一般
能纸垄 10000005 男 18 计11 健康
宰觅 10000002 男 23 计79 健康
宓顽芳 10000004 女 20 计17 健康
*/
#include <iostream>
#include <string>
using namespace std;
class StudentRecord
{
string stuName;
int stuNo;
string stuGender;
int stuAge;
string stuClass;
string stuHealth;
public:
StudentRecord() {}
StudentRecord(string stuName,
int stuNo,
string stuGender,
int stuAge,
string stuClass,
string stuHealth);
void print();
};
struct StudentNode
{
StudentRecord data;
StudentNode* next;
};
StudentNode* CreatNode(StudentRecord data)
{
StudentNode* newnode = new StudentNode;
if (newnode == NULL)
{
exit(-1);
}
newnode->data = data;
return newnode;
}
void ListPushBack(StudentNode** pplist, StudentRecord data)
{
StudentNode* head = *pplist;
StudentNode* newnode = CreatNode(data);
newnode->next = NULL;
if (head == NULL)
{
*pplist = newnode;
}
else
{
while (head->next)
{
head = head->next;
}
head->next = newnode;
}
}
void Connect(StudentNode** pplist)
{
StudentNode* Tail = *pplist;
while (Tail->next) {
Tail = Tail->next;
}
Tail->next = *pplist;
}
StudentNode* DeleteNode(StudentNode** pplist)
{
StudentNode* pre = *pplist;
StudentNode* cur = pre->next;
StudentNode* tail = cur->next;
cur->data.print();
pre->next = tail;
delete cur;
return tail;
}
int main()
{
StudentNode* pHead = NULL;
string stuName, stuGender, stuClass, stuHealth;
int stuNo, stuAge;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> stuName >> stuNo >> stuGender
>> stuAge >> stuClass >> stuHealth;
StudentRecord data(stuName,
stuNo,
stuGender,
stuAge,
stuClass,
stuHealth);
ListPushBack(&pHead, data);
}
Connect(&pHead);
for (int i = 0; i < n; i++) {
for (int j = 1; j < m - 1; j++) {
pHead = pHead->next;
}
pHead = DeleteNode(&pHead);
}
return 0;
}
StudentRecord::StudentRecord(string stuName, int stuNo, string stuGender,
int stuAge, string stuClass, string stuHealth) :
stuName(stuName), stuNo(stuNo), stuGender(stuGender),
stuAge(stuAge), stuClass(stuClass), stuHealth(stuHealth)
{}
void StudentRecord::print()
{
cout << stuName << " "
<< stuNo << " "
<< stuGender << " "
<< stuAge << " "
<< stuClass << " "
<< stuHealth << endl;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/haojingxin/nwafu-oj-topic-trial.git
git@gitee.com:haojingxin/nwafu-oj-topic-trial.git
haojingxin
nwafu-oj-topic-trial
NWAFU-OJ题目试做
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385