本人 AC 的第一道大模拟,写篇题解纪念一下。
这是一道要你写班级系统的题目。
对于每个学生,先用一个结构体写下。
struct stu{
int rk,s1,s2,s3,s4,sum,id;
string sid,cid,name;
double avg;//avg 为平均分
bool in()//内嵌输入函数
{
cin>>sid;
if(sid=="0") return false;
cin>>cid>>name>>s1>>s2>>s3>>s4;
sum=s1+s2+s3+s4;
avg=sum/4.00;//即时算出平均分
id=++_id;
return true;
}
};
vector<stu>v;//出于方便,使用 STL vector
可以发现,每次开始,都有一次 Welcome...
的语句。那我们使用一个函数存起来,方便使用。
void wel()
{
puts("Welcome to Student Performance Management System (SPMS).\n\n1 - Add\n2 - Remove\n3 - Query\n4 - Show ranking\n5 - Show Statistics\n0 - Exit\n");
}
那么主程序,我们可以这么写:
int main()
{
wel();
int op;
cin>>op;
while(1)
{
switch(op)
{
case 0://结束操作
exit(0);
case 1://添加学生
add();
break;
case 2://删除学生
del();
break;
case 3://查找学生
qry();
break;
case 4://显示排行
shw();
break;
case 5://显示成绩
stc();
}
wel();//每一次操作完都要一次输出
cin>>op;
}
}
使用之前的 in()
函数,判断为零时退出,否则判重来确定可否添加。
bool add_sid(string sid)
{
for(int i=0;i<v.size();i++)
{
if(sid==v[i].sid) return false;//判重(名字是可以重复的)
}
return true;
}
void add()
{
while(1)
{
puts("Please enter the SID, CID, name and four scores. Enter 0 to finish.");
stu t;
if(!t.in()) break;//退出
else if(!add_sid(t.sid)) puts("Duplicated SID.");//重复
else v.push_back(t);//合法
}
}
利用 vector 的 pop_back()
函数,再排一遍序,使得高效删除。
int del_rem(string s)
{
int res=0;
for(int i=0;i<v.size();i++)
{
if(v[i].name==s||v[i].sid==s)//匹配成功
{
swap(v[i--],v[(int)v.size()-1]);
v.pop_back();
res++;//删除人数增加
sort(v.begin(),v.end(),cmp);//再次排序
}
}
return res;
}
void del()
{
while(1)
{
puts("Please enter SID or name. Enter 0 to finish.");
string s;
cin>>s;
if(s=="0") break;
else cout<<del_rem(s)<<" student(s) removed."<<endl;//直接调用函数
}
}
最难的部分之一。
一边查找,一边处理排名。
同时浮点数精度会有误差,需要增加 1 0 − 5 10^{-5} 10−5。
void qry_fnd(string s)
{
vector<stu>vt;
for(int i=0;i<v.size();i++)
{
if(v[i].name==s||v[i].sid==s)//匹配成功
{
stu t;
t=v[i];
t.rk=1;
for(int j=0;j<v.size();j++)
{
if (v[j].sum>t.sum) t.rk++;//处理排名
}
vt.push_back(t);
}
}
for(int i=0;i<vt.size();i++)
{
cout<<vt[i].rk<<' '<<vt[i].sid<<' '<<vt[i].cid<<' '<<vt[i].name<<' '<<vt[i].s1<<' '<<vt[i].s2<<' '<<vt[i].s3<<' '<<vt[i].s4<<' '<<vt[i].sum<<' '<<fixed<<setprecision(2)<<vt[i].avg+exp/*这里的 exp 就是 10^(-5)*/<<endl;
}
}
void qry()
{
while(1)
{
puts("Please enter SID or name. Enter 0 to finish.");
string s;
cin>>s;
if(s=="0") break;
else qry_fnd(s);
}
}
输出那句话即可。
void shw()
{
puts("Showing the ranklist hurts students' self-esteem. Don't do that.");
}
最难的部分之二。
样例有误,一些地方是有空行的(详见代码)。
暴力查找计数,当然也要处理精度误差。
void stc_out(string s)
{
int s1s,s2s,s3s,s4s,s1p,s2p,s3p,s4p,s1f,s2f,s3f,s4f,p[10],k;//p_i 为通过 i 科人数
double s1a,s2a,s3a,s4a;
s1s=s2s=s3s=s4s=s1p=s2p=s3p=s4p=s1f=s2f=s3f=s4f=k=0;
memset(p,0,sizeof(p));
for(auto i:v)
{
if(i.cid==s||s=="0")
{
int ps=0;
k++;
s1s+=i.s1;
s2s+=i.s2;
s3s+=i.s3;
s4s+=i.s4;
if(i.s1>=60)
{
s1p++;
ps++;
}
else s1f++;
if(i.s2>=60)
{
s2p++;
ps++;
}
else s2f++;
if(i.s3>=60)
{
s3p++;
ps++;
}
else s3f++;
if(i.s4>=60)
{
s4p++;
ps++;
}
else s4f++;
p[ps]++;
}
}
s1a=(double)s1s/k;
s2a=(double)s2s/k;
s3a=(double)s3s/k;
s4a=(double)s4s/k;
printf("Chinese\nAverage Score: %.2f\nNumber of passed students: %d\nNumber of failed students: %d\n\nMathematics\nAverage Score: %.2f\nNumber of passed students: %d\nNumber of failed students: %d\n\nEnglish\nAverage Score: %.2f\nNumber of passed students: %d\nNumber of failed students: %d\n\nProgramming\nAverage Score: %.2f\nNumber of passed students: %d\nNumber of failed students: %d\n\nOverall:\nNumber of students who passed all subjects: %d\nNumber of students who passed 3 or more subjects: %d\nNumber of students who passed 2 or more subjects: %d\nNumber of students who passed 1 or more subjects: %d\nNumber of students who failed all subjects: %d\n\n",s1a+exp,s1p,s1f,s2a+exp,s2p,s2f,s3a+exp,s3p,s3f,s4a+exp,s4p,s4f,p[4],p[3]+p[4],p[2]+p[3]+p[4],p[1]+p[2]+p[3]+p[4],p[0]);
}
void stc()
{
puts("Please enter class ID, 0 for the whole statistics.");
string s;
cin>>s;
stc_out(s);
}
那么这样子整个代码就写完啦。
最后放上正确的样例:
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Duplicated SID.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter SID or name. Enter 0 to finish.
2 0022334455 1 Tom 59 72 60 81 272 68.00
Please enter SID or name. Enter 0 to finish.
1 0011223344 1 John 79 98 91 100 368 92.00
3 2423475629 2 John 60 80 30 99 269 67.25
Please enter SID or name. Enter 0 to finish.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter class ID, 0 for the whole statistics.
Chinese
Average Score: 69.00
Number of passed students: 1
Number of failed students: 1
Mathematics
Average Score: 85.00
Number of passed students: 2
Number of failed students: 0
English
Average Score: 75.50
Number of passed students: 2
Number of failed students: 0
Programming
Average Score: 90.50
Number of passed students: 2
Number of failed students: 0
Overall:
Number of students who passed all subjects: 1
Number of students who passed 3 or more subjects: 2
Number of students who passed 2 or more subjects: 2
Number of students who passed 1 or more subjects: 2
Number of students who failed all subjects: 0
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter SID or name. Enter 0 to finish.
1 student(s) removed.
Please enter SID or name. Enter 0 to finish.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter class ID, 0 for the whole statistics.
Chinese
Average Score: 59.50
Number of passed students: 1
Number of failed students: 1
Mathematics
Average Score: 76.00
Number of passed students: 2
Number of failed students: 0
English
Average Score: 45.00
Number of passed students: 1
Number of failed students: 1
Programming
Average Score: 90.00
Number of passed students: 2
Number of failed students: 0
Overall:
Number of students who passed all subjects: 0
Number of students who passed 3 or more subjects: 2
Number of students who passed 2 or more subjects: 2
Number of students who passed 1 or more subjects: 2
Number of students who failed all subjects: 0
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Showing the ranklist hurts students' self-esteem. Don't do that.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit