题解 | 体重排序

体重排序

https://www.nowcoder.com/practice/28a03ebdd3eb490dba057f7c15ae8610

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        string name;
        double weight;
        multimap<string, double> m;
        for (int i = 0; i < n; i++) {
            cin >> name >> weight;
            m.insert({name, weight});
        }
        vector<pair<string, double>> vec(m.begin(), m.end());
        sort(vec.begin(), vec.end(), [](const pair<string, double>& a, const pair<string, double>& b) { if (a.second != b.second) {
            return a.second < b.second;
        } else {
            return a.first < b.first;
        }
        });

        for (pair<string, double> i: vec) {
            cout << i.first << " ";
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

04-03 15:12
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务