当前位置: 首页> 娱乐> 八卦 > PAT甲级-1012 The Best Rank

PAT甲级-1012 The Best Rank

时间:2025/7/10 18:09:30来源:https://blog.csdn.net/weixin_74092648/article/details/142182579 浏览次数:0次

题目

 

题目大意

学生有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;
}

关键字:PAT甲级-1012 The Best Rank

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: