好吧,为了选择一个合适的数据结构纠结了很久。。。。因为要查排名,然后就想到了Treap,然后发现自己的Treap的板子写的不好,于是把Treap用template改写了一遍,然后发现用不上.......
最后看的标程,感觉标程的写法还是很不错的,比较短,而且也不是很难理解,就是...每一次查rank都是从头扫一遍,复杂度几乎都是O(n)的,不过因为上限只有100,数据量也不算很大,也能过去。但是我想用更快的数据结构呀!教练!
WA了2发,发现是在&&和||的组合判断式的时候少打了括号,导致答案错误,因为&&的优先级比||高嘛
写一个模拟题练练手喽
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const string menu="Welcome to Student Performance Management System (SPMS).\n\n1 - Add\n2 - Remove\n3 - Query\n4 - Show ranking\n5 - Show Statistics\n0 - Exit\n\n",
course[]={"Chinese", "Mathematics", "English", "Programming"};
int choice,rev[1010],n,getrank(int);
bool valid();
void add(),rem(),que(),shw();
struct Stu{
string name,SID;
int CID,s[5];
}arr[1010];
int main(){
ios_base::sync_with_stdio(false);
cout.setf(ios_base::fixed);
cout.precision(2);
cout<<menu;
while(cin>>choice&&choice){
if(choice==1)
add();
else if(choice==2)
rem();
else if(choice==3)
que();
else if(choice==4)
cout<<"Showing the ranklist hurts students' self-esteem. Don't do that.\n";
else
shw();
cout<<menu;
}
return 0;
}
bool valid(){
for(int i=0;i<n;++i)
if(!rev[i]&&arr[i].SID==arr[n].SID)
return false;
return true;
}
void add(){
cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish.\n";
while(cin>>arr[n].SID&&!(arr[n].SID.size()==1&&arr[n].SID[0]=='0')){
cin>>arr[n].CID>>arr[n].name;
arr[n].s[4]=0;
for(int i=0;i<4;++i)
cin>>arr[n].s[i],arr[n].s[4]+=arr[n].s[i];
if(valid())
++n;
else
cout<<"Duplicated SID.\n";
cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish.\n";
}
}
void rem(){
cout<<"Please enter SID or name. Enter 0 to finish.\n";
string str;
int num=0;
while(cin>>str&&!(str.size()==1&&str[0]=='0')){
for(int i=0;i<n;++i)
if(!rev[i]&&(arr[i].SID==str||arr[i].name==str))
rev[i]=true,++num;
cout<<num<<" student(s) removed.\n";
num=0;
cout<<"Please enter SID or name. Enter 0 to finish.\n";
}
}
void que(){
cout<<"Please enter SID or name. Enter 0 to finish.\n";
string str;
while(cin>>str&&!(str.size()==1&&str[0]=='0')){
for(int i=0;i<n;++i)
if(!rev[i]&&(arr[i].SID==str||arr[i].name==str))
cout<<getrank(i)<<' '<<arr[i].SID<<' '<<arr[i].CID<<' '<<arr[i].name<<' '<<arr[i].s[0]<<' '<<arr[i].s[1]<<' '<<arr[i].s[2]<<' '<<arr[i].s[3]<<' '<<arr[i].s[4]<<' '<<(arr[i].s[4]/4.0+1E-5)<<endl;
cout<<"Please enter SID or name. Enter 0 to finish.\n";
}
}
void shw(){
cout<<"Please enter class ID, 0 for the whole statistics.\n";
int cid,total=0,s[4]{},sm[4]{},cnt[5]{};
cin>>cid;
for(int i=0,k=0;i<n;++i)
if(!rev[i]&&(!cid||arr[i].CID==cid)){
++total;
k=0;
for(int j=0;j<4;++j){
if(arr[i].s[j]>=60)
++sm[j],++k;
s[j]+=arr[i].s[j];
}
++cnt[k];
}
for(int i=0;i<4;++i)
cout<<course[i]<<"\nAverage Score: "<<(total==0?00.00:s[i]*1.0/total+1E-5)<<"\nNumber of passed students: "<<sm[i]<<"\nNumber of failed students: "<<total-sm[i]<<endl<<endl;
cout<<"Overall:\nNumber of students who passed all subjects: "<<cnt[4]
<<"\nNumber of students who passed 3 or more subjects: "<<cnt[4]+cnt[3]
<<"\nNumber of students who passed 2 or more subjects: "<<cnt[4]+cnt[3]+cnt[2]
<<"\nNumber of students who passed 1 or more subjects: "<<cnt[4]+cnt[3]+cnt[2]+cnt[1]
<<"\nNumber of students who failed all subjects: "<<cnt[0]<<endl<<endl;
}
int getrank(int ind){
int ran=0;
for(int i=0;i<n;++i)
if(!rev[i]&&arr[i].s[4]>arr[ind].s[4])
++ran;
return ran+1;
}