Title: sorting problems.
Idea: Unordered_map is used to store the mapping between the examinee’s name and the score information structure. The score is initialized to – 1. Each score is updated when reading the data. Finally, the final score is calculated and the qualified students are saved into vector, and then sorted. It is important to note thatWhen calculating final results, remember “G must be rounded up to an integer”. The summing up function is summarized here.
Code:
#include <iostream> #include <string> #include <unordered_map> #include <vector> #include <algorithm> #include <cmath> #include <fstream> using namespace std; struct Student{ string id; int Gp,Gm,Gf,Gtot; Student():id(""),Gp(-1),Gm(-1),Gf(-1),Gtot(0){} }; unordered_map<string,Student> mp; vector<Student> stu; bool cmp(Student a,Student b) { if(a.Gtot!=b.Gtot) return a.Gtot>b.Gtot; else return a.id<b.id; } int main() { //ifstream cin("pat.txt"); int p,m,f; cin>>p>>m>>f; string id; int score; for(int i=0;i<p;i++){ cin>>id>>score; mp[id].id=id; mp[id].Gp=score; } for(int i=0;i<m;i++){ cin>>id>>score; mp[id].id=id; mp[id].Gm=score; } for(int i=0;i<f;i++){ cin>>id>>score; mp[id].id=id; mp[id].Gf=score; } for(auto it:mp){ Student st=it.second; if(st.Gm>st.Gf) st.Gtot=round(st.Gm*0.4+st.Gf*0.6);//Pay attention to four homes, five entries.else st.Gtot=st.Gf; if(st.Gp>=200 && st.Gtot>=60) stu.push_back(st); } sort(stu.begin(),stu.end(),cmp); for(auto it:stu) cout<<it.id<<" "<<it.Gp<<" "<<it.Gm<<" "<<it.Gf<<" "<<it.Gtot<<"\n"; return 0; }