题目
题目大意
学生有C、M、E三个成绩,A是这三个成绩的平均值。要求对每个学生的C、M、E、A分别排名,取这4项的最高排名为最优排名。如果一个学生有多项排名一样,按照A > C > M > E的优先级输出最优排名。
输入给出学生人数和查询人数,以及学生信息和需要查询的id。要求输出每个查询的最优排名及对应的科目,如果在学生信息中找不到要查询的id,就输出N/A。
思路
定义结构体,录入信息。再按照A C M E分别进行排序,用以学生id为键,以各科排名为值(字符串形式)的map存储排序结果,找到单科成绩的排名。之后再对应查询数组遍历map,找到每个学生对应的最优排名。注意如果不同学生的成绩相同,那么排名也必须相同,按照“1 1 1 4 5”这样的排名方式。
代码
#include <iostream>
#include <map>
#include <vector>
#include <cctype>
#include <algorithm>
using namespace std;struct Stu{int id;int c;int m;int e;int a;
}; // 定义一个学生结构体bool cmpa(Stu stu1, Stu stu2){return stu1.a > stu2.a;
}
bool cmpc(Stu stu1, Stu stu2){return stu1.c > stu2.c;
}
bool cmpm(Stu stu1, Stu stu2){return stu1.m > stu2.m;
}
bool cmpe(Stu stu1, Stu stu2){return stu1.e > stu2.e;
} // 自定义排序规则int main(){int n, m;cin >> n >> m;vector<Stu> stu(n);for (int i = 0; i < n; i++){cin >> stu[i].id >> stu[i].c >> stu[i].m >> stu[i].e;stu[i].a = (stu[i].c + stu[i].m + stu[i].e) / 3;}vector<int> res(m);for (int i = 0; i < m; i++){cin >> res[i];}map<int, string> mp;sort(stu.begin(), stu.end(), cmpa);int k;for (int i = 0; i < n; i++){if (i == 0){mp[stu[i].id] += '1';k = 1;}else{if (stu[i].a == stu[i-1].a){mp[stu[i].id] += k + '0';}else{mp[stu[i].id] += i + 1 + '0';k = i + 1;}}mp[stu[i].id] += 'A';} // 对A排名sort(stu.begin(), stu.end(), cmpc);for (int i = 0; i < n; i++){if (i == 0){mp[stu[i].id] += '1';k = 1;}else{if (stu[i].c == stu[i-1].c){mp[stu[i].id] += k + '0';}else{mp[stu[i].id] += i + 1 + '0';k = i + 1;}}mp[stu[i].id] += 'C';} // 对C排名sort(stu.begin(), stu.end(), cmpm);for (int i = 0; i < n; i++){if (i == 0){mp[stu[i].id] += '1';k = 1;}else{if (stu[i].m == stu[i-1].m){mp[stu[i].id] += k + '0';}else{mp[stu[i].id] += i + 1 + '0';k = i + 1;}}mp[stu[i].id] += 'M';} // 对M排名sort(stu.begin(), stu.end(), cmpe);for (int i = 0; i < n; i++){if (i == 0){mp[stu[i].id] += '1';k = 1;}else{if (stu[i].e == stu[i-1].e){mp[stu[i].id] += k + '0';}else{mp[stu[i].id] += i + 1 + '0';k = i + 1;}}mp[stu[i].id] += 'E';} // 对E排名for (int i = 0; i < m; i++){int flag = 0;for (auto it = mp.begin(); it != mp.end(); it++){if (it->first == res[i]){flag = 1;string s = it->second;int min = 3000;int pos;for (int j = 0; j < (int)s.size(); j++){if (isdigit(s[j]) && min > s[j]){min = s[j];pos = j;}}cout << s[pos] << " " << s[pos + 1] << endl;}}if (flag == 0){cout << "N/A" << endl;}} // 找到最优排名并输出结果return 0;
}