博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
8、sort排序中比较函数的几种应用方式
阅读量:4562 次
发布时间:2019-06-08

本文共 1567 字,大约阅读时间需要 5 分钟。

1、待排序中的元素作数组的下标或map的键值

例题:PAT甲级_1141 PAT Ranking of Institutions

 

#include
using namespace std;const int maxn = 100010;struct Node { int personNum = 0; int scoreB = 0, scoreA = 0, scoreT = 0; int score;};map
mp;vector
ansSchoolName;bool cmp(string a, string b) { if (mp[a].score != mp[b].score) return mp[a].score > mp[b].score; else return mp[a].personNum < mp[b].personNum;} int main() { int n; scanf("%d", &n); int score; string id, schoolName; for (int i = 0; i < n; i++) { cin >> id >> score >> schoolName; transform(schoolName.begin(), schoolName.end(), schoolName.begin(), ::tolower); mp[schoolName].personNum++; if (id[0] == 'A') mp[schoolName].scoreA += score; else if (id[0] == 'B') mp[schoolName].scoreB += score; else mp[schoolName].scoreT += score; } for (auto it = mp.begin(); it != mp.end(); it++) { ansSchoolName.push_back(it->first); it->second.score = (int)(it->second.scoreA + it->second.scoreB / 1.5 + it->second.scoreT*1.5); } sort(ansSchoolName.begin(), ansSchoolName.end(), cmp); int r = 1; printf("%d", ansSchoolName.size()); for (int i = 0; i < ansSchoolName.size(); i++) { if (i > 0 && mp[ansSchoolName[i]].score != mp[ansSchoolName[i - 1]].score) { r = i + 1; } printf("%d %s %d %d\n", r, ansSchoolName[i].c_str(), mp[ansSchoolName[i]].score, mp[ansSchoolName[i]].personNum); } return 0;}

 

转载于:https://www.cnblogs.com/fuqia/p/9606830.html

你可能感兴趣的文章
【2019-08-20】有点目标,有点计划,有点目的
查看>>
【2019-09-10】美,真的跟年龄无关
查看>>
【2019-09-28】少,但更好
查看>>
【2019-09-13】耐心观察是一种技能
查看>>
mysql数据库2-常用命令
查看>>
安卓开发环境搭建(转)
查看>>
Harris角点检测
查看>>
Struts2的处理流程及为Action的属性注入值
查看>>
设计中最常用的CSS选择器
查看>>
Maven项目打包成可执行Jar文件
查看>>
nginx http proxy 正向代理
查看>>
对BFC的总结
查看>>
23醒
查看>>
Google Hack的一些整理
查看>>
[贪心] JZOJ P3757 随机生成器
查看>>
Codeforces Round #370 (Div. 2)(简单逻辑,比较水)
查看>>
操作系统进程调度算法
查看>>
less与sass的区别点
查看>>
event.keycode值大全
查看>>
array and ram
查看>>