牛客网 KY117 奥运排序问题
发布日期:2021-07-22 07:29:02
浏览次数:5
分类:技术文章
本文共 2479 字,大约阅读时间需要 8 分钟。
最直观的解法就是四个排序全部进行一次,记录最好的一次输出,解法如下:
#include#include #include using namespace std;class country { public: int gold; int medal; int population; int num; //国家编号 float gold_population; //金牌人口比例 float medal_population; //奖牌人口比例 int rank[5]; //记录各情况下排名 bool output; //标记是否输出};bool cmp_gold(country a, country b) { return a.gold > b.gold;}bool cmp_medal(country a, country b) { return a.medal > b.medal;}bool cmp_gp(country a, country b) { return a.gold_population > b.gold_population;}bool cmp_mp(country a, country b) { return a.medal_population > b.medal_population;}bool cmp_num(country a, country b) { return a.num < b.num;}country all[250];int main(){ int n, m; while (cin >> n >> m) { if (n == 0) break; for (int i = 0; i < n; ++i) { cin >> all[i].gold >> all[i].medal >> all[i].population; all[i].gold_population = (float)all[i].gold / (float)all[i].population; all[i].medal_population = (float)all[i].medal / (float)all[i].population; all[i].num = i; //记录国家编号,方便最后按需输出 all[i].output = false; } for (int i = 0; i < m; ++i) { int num; cin >> num; all[num].output = true; //要求输出排序则置标记为true } sort(all, all + n, cmp_gold); //按金牌总数排序 int k = 1; for (int i = 0; i < n; ++i) { if (i > 0 && all[i].gold != all[i - 1].gold) { k = i + 1; all[i].rank[1] = k; } else { all[i].rank[1] = k; } } sort(all, all + n, cmp_medal); //按奖牌总数排序 k = 1; for (int i = 0; i < n; ++i) { if (i > 0 && all[i].medal != all[i - 1].medal) { k = i + 1; all[i].rank[2] = k; } else { all[i].rank[2] = k; } } sort(all, all + n, cmp_gp); //按金牌人口比例排序 k = 1; for (int i = 0; i < n; ++i) { if (i > 0 && all[i].gold_population != all[i - 1].gold_population) { k = i + 1; all[i].rank[3] = k; } else { all[i].rank[3] = k; } } sort(all, all + n, cmp_mp); //按奖牌人口比例排序 k = 1; for (int i = 0; i < n; ++i) { if (i > 0 && all[i].medal_population != all[i - 1].medal_population) { k = i + 1; all[i].rank[4] = k; } else { all[i].rank[4] = k; } } sort(all, all + n, cmp_num); //再按国家顺序排序 for (int i = 0; i < n; ++i) { if (all[i].output) { int r = all[i].rank[1], method = 1; //找最好的输出 for (int j = 2; j < 5; ++j) { if (all[i].rank[j] < r) { r = all[i].rank[j]; method = j; } } cout << r << ":" << method << endl; } } }}
转载地址:https://blog.csdn.net/m0_45689014/article/details/113903664 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年03月24日 19时44分44秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux(六)——时间
2019-04-26
Linux(七)——查找指令
2019-04-26
Linux(九)——组
2019-04-26
Python(四十四)——转义字符与原字符
2019-04-26
Python(四十六)——抽象基类
2019-04-26
Python(四十七)——is和==的区别
2019-04-26
基于pytorch的深度学习的第二章下载失败解决方式
2019-04-26
关于qt5用 designer 查看代码时报错的处理方式
2019-04-26
python 中使用 opencv 上传图片
2019-04-26
C1认证:任务01-修改游戏存档
2019-04-26
C1认证:任务02-计算机网络协议
2019-04-26
不跳步骤的新手python爬虫教程(一)
2019-04-26
不跳步骤的新手python爬虫教程(二)
2019-04-26
秃头程序员保姆教程:Spring框架自学之路(一)
2019-04-26
MATLAB与Mysql数据库连接并数据交换(基于ODBC)
2019-04-26
Java生成随机32长字符串:实现MySQL表主键ID、激活码不重复
2019-04-26
TxQueryRunner类的使用
2019-04-26