没啥好说的,按题意对数据排序输出即可。
STL:vector,string.用cin/cout可能会超时,改成scanf和printf即可。
#include"iostream" #include"algorithm" #include"vector" using namespace std; struct Student { string id; int G1,G2,sum; int type; } ; bool cmp(const Student& a,const Student& b) { if(a.type != b.type) return a.type < b.type; else if(a.sum != b.sum) return a.sum > b.sum; else if(a.G1 != b.G1) return a.G1 > b.G1; else return a.id < b.id; } int main() { int N,L,H,g1,g2; string str; scanf("%d%d%d",&N,&L,&H); vector<Student> vec; Student stu; for(int i = 0; i < N; ++i) { cin>>str; scanf("%d%d",&g1,&g2); if(g1 >= L && g2 >= L) { stu.id = str; stu.G1 = g1; stu.G2 = g2; stu.sum = g1+g2; if(g1 >= H && g2 >= H ) stu.type = 1; else if(g1 >= H && g2 < H) stu.type = 2; else if(g1 <= H && g2 <= H && g1 >= g2) stu.type = 3; else stu.type = 4; vec.push_back(stu); } } sort(vec.begin(),vec.end(),cmp); cout<<vec.size()<<endl; for(auto it = vec.begin(); it != vec.end(); ++it) cout<<it->id<<" "<<it->G1<<" "<<it->G2<<endl; return 0; }
原文:https://www.cnblogs.com/keep23456/p/12313855.html