题解 | #排名#
排名
https://www.nowcoder.com/practice/f7c80167c5b04fd2ac4b6d7080a73f01
#include <iostream> #include <cstring> #include <algorithm> #include <vector> using namespace std; struct Stu { string id;//准考证号 int qcount;//解题数量 vector<int> qno;//题号 int totalScore; Stu(string id, int qcount): id(id), qcount(qcount) { qno.resize(qcount); } Stu() {} }; class Room { private: int n; //考生人数 int m; //题目数量 int line;//分数线 vector<int> qscore;//题目分值 vector<Stu> stu; //考生信息 public: Room(int n, int m, int line): n(n), m(m), line(line) { qscore.resize(m); stu.resize(n); } void setQscore(vector<int> &other) { qscore=other; } void setStu(vector<Stu> &other) { stu=other; } void calcuTotalScore() { for(int i=0;i<stu.size();i++) { int total=0; for(int j=0;j<stu[i].qcount;j++) { int q=stu[i].qno[j]; total+=qscore[q-1]; } stu[i].totalScore=total; } } static bool cmp(Stu a, Stu b) { if(a.totalScore != b.totalScore ) { return a.totalScore > b.totalScore; } else return a.id < b.id; } void func() { for(int i=stu.size()-1;i>=0;i--) { if(stu[i].totalScore<line) { stu.erase(stu.begin()+i); } } sort(stu.begin(),stu.end(),cmp); } void display() { if(stu.size()==0) { cout<<"0"<<endl; } else { cout<<stu.size()<<endl; for(int i=0;i<stu.size();i++) { cout<<stu[i].id<<" "<<stu[i].totalScore<<endl; } } } }; int main() { int n0; int k=0; vector<Room> room; while(cin>>n0 && n0!=0) { int m0, line0; cin>>m0>>line0; Room *room0=new Room(n0,m0,line0); // vector<Room> room; room.push_back(*room0); int qscore0; vector<int> qscore1(m0); for(int i=0;i<m0;i++) { cin>>qscore0; qscore1[i]=qscore0; } room[k].setQscore(qscore1); string id0; int qcount0; vector<Stu> stu1(n0); for(int i=0;i<n0;i++) { cin>>id0>>qcount0; Stu *stu0 = new Stu(id0,qcount0); for(int j=0;j<qcount0;j++) { cin>>stu0->qno[j]; } stu1[i]=*stu0; } room[k].setStu(stu1); k++; } for(int i=0;i<room.size();i++) { room[i].calcuTotalScore(); room[i].func(); room[i].display(); } return 0; }