编程-奥林匹克运动会
2012伦敦奥运会上,大家都非常关注奖牌榜的情况,现在我们假设奖牌榜的排名规则如下:
1、首先gold medal数量多的排在前面;
2、其次silver medal数量多的排在前面;
3、然后bronze medal数量多的排在前面;
4、若以上三个条件仍无法区分名次,则以国家名称的字典序排定。
我们假设国家名称不超过20个字符、各种奖牌数不超过100,且大于等于0。
解答要求时间限制:1000ms, 内存限制:100MB
输入
第一行输入一个整数N(0<N<21),代表国家数量;
然后接下来的N行,每行包含一个字符串Namei表示每个国家的名称,和三个整数Gi、Si、Bi表示每个获得的gold medal、silver medal、bronze medal的数量,以空格隔开,如(China 51 20 21),具体见样例输入。
输出
输出奖牌榜的依次顺序,只输出国家名称,各占一行,具体见样例输出。
样例
输出样例 1
China Rusia France Japan England
解法1:
#include<set> //自定义set的key typedef struct OlympicKey { uint64_t gold; uint64_t silver; uint64_t bronze; string nation; } OlympicKey; //set的比较函数 struct cmp_key { bool operator()(const OlympicKey &k1, const OlympicKey &k2) const {
//降序排列 if(k1.gold > k2.gold) return true; if(k1.gold < k2.gold) return false; if(k1.silver > k2.silver) return true; if(k1.silver < k2.silver) return false; if(k1.bronze > k2.bronze) return true; if(k1.bronze < k2.bronze) return false; if(k1.nation > k2.nation) return false; if(k1.nation < k2.nation) return true; return true; }; }; int main() { // please define the C++ input here. For example: int a,b; cin>>a>>b; set<OlympicKey, cmp_key> OlympicSet; int num; cin >> num; // please finish the function body here. while (num--) { string nation; int gold, silver, bronze; cin >> nation >> gold >> silver >> bronze; OlympicKey key; key.gold = gold; key.silver = silver; key.bronze = bronze; key.nation = nation; OlympicSet.insert(key); } // please define the C++ output here. For example:cout<<____<<endl; for (auto &it: OlympicSet) { cout << it.nation << endl; } return 0; }
解法2
#include<set> //自定义set的key typedef struct OlympicKey { uint64_t gold; uint64_t silver; uint64_t bronze; string nation; OlympicKey(uint64_t i1, uint64_t i2, uint64_t i3, string s1) { gold = i1; silver = i2; bronze = i3; nation = s1; } //这里必须为<运算符才能保证语法正确 bool operator < (const OlympicKey &k1) const { //降序排列 if (gold != k1.gold) return gold > k1.gold; if (silver != k1.silver) return silver > k1.silver; if (bronze != k1.bronze) return bronze > k1.bronze; return nation > k1.nation; } } OlympicKey; //typedef struct tag_OlympicKey OlympicKey; int main() { // please define the C++ input here. For example: int a,b; cin>>a>>b; set<OlympicKey> OlympicSet; int num; cin >> num; // please finish the function body here. while (num--) { string nation; int gold, silver, bronze; cin >> nation >> gold >> silver >> bronze; OlympicSet.insert(OlympicKey(gold, silver, bronze, nation)); } // please define the C++ output here. For example:cout<<____<<endl; for (auto &it: OlympicSet) { cout << it.nation << endl; } return 0; }

浙公网安备 33010602011771号