简要题意

这道题就是要你维护一个学生成绩管理系统。

代码实现

程序设计

为了方便输出,我们定义了 println 函数:

void println(string s){
cout<<s<<'\n';
}

学生信息维护我用了一个结构体 Student:

struct Student{
string sid,name;
int cid;
int chinese,mathematics,english,programming;
bool operator==(const Student &x) const{
return sid==x.sid && cid==x.cid && chinese==x.chinese && mathematics==x.mathematics && english==x.english && programming == x.programming;
}
};

存储学生我用了一个 vector(主存储)和一个 map(辅助存储,用于判断是否有重复的 SID):

vector<Student> v;
map<string,bool> duplicated;

然后就是主程序了:

#define judge(a,func) case a:func();break;
signed main(){
// freopen("1.out","w",stdout);
while(1){
main_menu();
int op;
cin>>op;
switch(op){
judge(1,add);
judge(2,remove);
judge(3,query);
judge(4,show_ranking);
judge(5,show_statisitics);
case 0:return 0;
}
}
}

主菜单部分

输出主菜单的部分,并不需要太大的难度。

void main_menu(){
println("Welcome to Student Performance Management System (SPMS).");
println("");
println("1 - Add");
println("2 - Remove");
println("3 - Query");
println("4 - Show ranking");
println("5 - Show Statistics");
println("0 - Exit");
println("");
}

添加学生部分

这一部分我们先读入学生信息,判 \(0\) 后再判重,如果都通过了就加进去。然后尾递归。

void add(){
println("Please enter the SID, CID, name and four scores. Enter 0 to finish.");
string sid;
cin>>sid;
if(sid=="0")return;
Student student;
student.sid=sid;
cin>>student.cid>>student.name>>student.chinese>>student.mathematics>>student.english>>student.programming;
if(duplicated[sid]){
println("Duplicated SID.");
}
else{
duplicated[sid]=1;
v.push_back(student);
}
add();
}

删除学生部分

这一部分先遍历所有学生,如果存在满足要求的学生就放进一个栈里。最后一个一个将栈中的元素取消存在 SID 标并删除。

void remove(){
println("Please enter SID or name. Enter 0 to finish.");
string str;
cin>>str;
stack<vector<Student>::iterator> removing;
if(str=="0")return;
int tot=0;
for(auto ite=v.begin();ite!=v.end();ite++){
if((*ite).sid==str || (*ite).name==str){
tot++;
duplicated[(*ite).sid]=0;
removing.push(ite);
}
}
while(!removing.empty()){
v.erase(removing.top());
removing.pop();
}
cout<<tot;
println(" student(s) removed.");
remove();
}

查询学生部分

码量较大的部分。我们先遍历所有学生,如果有满足条件的话:先将这个学生所在的班级的所有学生取出来,然后排一遍序,找到这个学生的排名,再输出信息。

int total(Student x){
return x.chinese+x.english+x.mathematics+x.programming;
} void query(){
println("Please enter SID or name. Enter 0 to finish.");
string str;
cin>>str;
if(str=="0")return;
for(Student i : v){
if(i.sid==str || i.name==str){
vector<Student> same_class;
for(Student j : v){
same_class.push_back(j);
}
sort(same_class.begin(),same_class.end(),[&](const Student &x,const Student &y){
return x.chinese+x.mathematics+x.english+x.programming>y.chinese+y.mathematics+y.english+y.programming;
});
int ret=0;
for(vector<Student>::size_type j=0;j<same_class.size();j++){
if(j>0&&total(same_class[j])==total(same_class[j-1])){}
else ret=j+1;
if(same_class[j]==i){
break;
}
}
cout<<ret<<' '<<i.sid<<' '<<i.cid<<' '<<i.name<<' ';
cout<<i.chinese<<' '<<i.mathematics<<' '<<i.english<<' '<<i.programming<<' ';
cout<<(i.chinese+i.mathematics+i.english+i.programming)<<' ';
printf("%.2lf\n",((double)((i.chinese+i.mathematics+i.english+i.programming))/4.0)+1e-5);
}
}
query();
}

显示排名部分

这一部分最简单,由于本题不需要我们输出排名,我们只需要输出提示信息即可。

void show_ranking(){
println("Showing the ranklist hurts students' self-esteem. Don't do that.");
}

班级统计部分

码量最大的部分。但是思维偏简单,直接遍历班级的学生(如果 CID 时 \(0\) 就是所有的学生)

,找到没有及格的和及格的,进行计数即可。

void show_statisitics(){
println("Please enter class ID, 0 for the whole statistics.");
int cid;cin>>cid;
int passed=0,failed=0;
double aver=0;
int cnt=0;
for(Student i : v){
if(i.cid!=cid&&cid!=0)continue;
cnt++;
}
{
println("Chinese");
for(Student i : v){
if(i.cid!=cid&&cid!=0)continue;
aver += i.chinese;
passed += (i.chinese >= 60);
failed += (i.chinese < 60);
}
aver /= cnt;
cout<<"Average Score: ";
printf("%.2lf\n",aver+1e-5);
cout<<"Number of passed students: "<<passed<<'\n';
cout<<"Number of failed students: "<<failed<<'\n';
println("");
}
{
aver=0;failed=0;passed=0;
println("Mathematics");
for(Student i : v){
if(i.cid!=cid&&cid!=0)continue;
aver += i.mathematics;
passed += (i.mathematics >= 60);
failed += (i.mathematics < 60);
}
aver /= cnt;
cout<<"Average Score: ";
printf("%.2lf\n",aver+1e-5);
cout<<"Number of passed students: "<<passed<<'\n';
cout<<"Number of failed students: "<<failed<<'\n';
println("");
}
{
aver=0;failed=0;passed=0;
println("English");
for(Student i : v){
if(i.cid!=cid&&cid!=0)continue;
aver += i.english;
passed += (i.english >= 60);
failed += (i.english < 60);
}
aver /= cnt;
cout<<"Average Score: ";
printf("%.2lf\n",aver+1e-5);
cout<<"Number of passed students: "<<passed<<'\n';
cout<<"Number of failed students: "<<failed<<'\n';
println("");
}
{
aver=0;failed=0;passed=0;
println("Programming");
for(Student i : v){
if(i.cid!=cid&&cid!=0)continue;
aver += i.programming;
passed += (i.programming >= 60);
failed += (i.programming < 60);
}
aver /= cnt;
cout<<"Average Score: ";
printf("%.2lf\n",aver+1e-5);
cout<<"Number of passed students: "<<passed<<'\n';
cout<<"Number of failed students: "<<failed<<'\n';
println("");
}
println("Overall:");
int all=0,one=0,two=0,three=0,failed_all=0;
for(Student i : v){
if(i.cid!=cid&&cid!=0)continue;
int ret = (int)(i.chinese>=60)+(int)(i.mathematics>=60)+(int)(i.english>=60)+(int)(i.programming>=60);
if(ret>=1)one++;
if(ret>=2)two++;
if(ret>=3)three++;
if(ret>=4)all++;
if(ret==0)failed_all++;
}
cout<<"Number of students who passed all subjects: "<<all<<'\n';
cout<<"Number of students who passed 3 or more subjects: "<<three<<'\n';
cout<<"Number of students who passed 2 or more subjects: "<<two<<'\n';
cout<<"Number of students who passed 1 or more subjects: "<<one<<'\n';
cout<<"Number of students who failed all subjects: "<<failed_all<<'\n';
println("");
}

写在最后

这一道题,个人感觉并不是很难,\(30\) 分钟码代码 + \(20\) 分调试就做出来了。(最后发现时引号问题和删除问题)写完之后头脑清醒很多,收获了一个 UVA 调试工具 uDebug,还提升了英语水平(没有中文翻译,纯看英文题面)。

代码见此

UVA12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)的更多相关文章

  1. 【例题4-6 uva12412】A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 训练编程的题. 原题中没有除0的数据,所以别担心你的代码是因为除0错了. 多半跟我一样. 也是因为没有+eps 就是比如你要算tot ...

  2. UVA12412 师兄帮帮忙 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang) 题解

    Content 自己去看题面去. Solution 算不上很繁琐的一道大模拟. 首先,既然是输出 \(0\) 才退出,那么在此之前程序应当会执行菜单 \(\Rightarrow\) 子操作 \(\Ri ...

  3. UVA 12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

    题目链接:https://vjudge.net/problem/UVA-12412 题目大意 略. 分析 比较大规模的模拟,注意输入输出,浮点数精度,还有排名相同的输出顺序,还有一些边界情况处理. 代 ...

  4. A Typical Homework(学生信息管理系统)

    A Typical Homework(a.k.a Shi Xiong Bang Bang Mang) Hi, I am an undergraduate student in institute of ...

  5. 五、Pandas玩转数据

    Series的简单运算 import numpy as np import pandas as pd s1=pd.Series([1,2,3],index=['A','B','C']) print(s ...

  6. Radio Basics for RFID

    Radio Basics for RFID The following is excerpted from Chapter 3: Radio Basics for UHF RFID from the ...

  7. [IR] Compression

    关系:Vocabulary vs. collection size Heaps’ law: M = kTbM is the size of the vocabulary, T is the numbe ...

  8. php中文转拼音2

    <?php /** * strtopinyin.php * * @name 汉字字符转拼音 * @author Kudosharry * @version v1.0 * */ class Str ...

  9. PTA|团体程序设计天梯赛-练习题目题解锦集(C/C++)(持续更新中……)

    PTA|团体程序设计天梯赛-练习题目题解锦集(持续更新中) 实现语言:C/C++:      欢迎各位看官交流讨论.指导题解错误:或者分享更快的方法!! 题目链接:https://pintia.cn/ ...

  10. Python 小白的新手教程(一)

    本文是 python 入门级别的基础知识,包括数据类型和变量.输入输出.字符串和编码.list tuple dict set .条件判断.循环.函数.切片 迭代 列表生成器 生成器 迭代器等. 参考课 ...

随机推荐

  1. 知识图谱-生物信息学-医学顶刊论文(Bioinformatics-2021)-KG4SL:用于人类癌症综合致死率预测的知识图神经网络

    5.(2021.7.12)Bioinformatics-KG4SL:用于人类癌症综合致死率预测的知识图神经网络 论文标题:KG4SL: knowledge graph neural network f ...

  2. LcdToos如何在线对屏进行读写指令调试

    在实际屏调试过程中,工程师经常需要对屏的寄存器频繁进行参数修改和读取测试,LcdTools针对这个做了很好的支持,可以在线进行指令调试,大大提高调试效率. 打开点屏工程,连接PX01并使模组上电点亮. ...

  3. Python 包(package)

    在比较大型的项目中常常需要编写.用到大量的模块,此时我们可以使用包(Package)来管理这些模块. (一)什么是包? Python包,就是里面装了一个__init__.py文件的文件夹. __ini ...

  4. MySQL该使用哪种CPU架构服务器?

    1. 摘要 近期,阿里云推出基于 ARM 架构的 RDS MySQL 和 RDS PostgreSQL 实例,现处于邀测阶段,阿里云宣传 ARM 架构的亮点是:在价格下降13%的基础上,平均性能 AR ...

  5. 谷歌、微软、Meta?谁才是 Python 最大的金主?

    你知道维护 Python 这个大规模的开源项目,每年需要多少资金吗? 答案是:约 200 万美元! PSF(Python 软件基金会)在 2022 年 6 月发布了 2021 的年度报告,其中披露了以 ...

  6. 解决windows installation failed! Error: 无法访问 Windows Installer 服务

    这种错误,是因为没有开启winodws Installer这个服务导致的,在开始菜单搜索"服务",找到windows Installer 这个服务,右键--属性--把启动类型 选成 ...

  7. 移动 VR 开发时要避免的 PC 渲染技术

    更新:本文是为 Quest 1 开发人员编写的.虽然 Quest 2 建立在相同的架构上,但现在更容易为阴影贴图(以及其他需要从先前渲染过程中生成的纹理读取的简单技术)做预算. 尽管移动芯片组可以支持 ...

  8. Zabbix技术分享——docker组件编译使用教程

    docker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源,它可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的Linux机器上,还可以实现 ...

  9. 在实际应用中联合体union的妙用

    关键字union,又称为联合体.共用体,联合体的声明和结构体类似,但是它的行为方式又和结构体不同,这里的行为方式主要指的是其在内存中的体现,结构体中的成员每一个占据不同的内存空间,而联合体中的所有成员 ...

  10. 3.7:基于Weka的K-means聚类的算法示例

    〇.目标 1.使用Weka平台,并在该平台使用数据导入.可视化等基本操作: 2.对K-means算法的不同初始k值进行比较,对比结果得出结论. 一.打开Weka3.8并导入数据 二.导入数据 三.Si ...