题解 | 体重排序

体重排序

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

要考虑排序时间的问题

#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct stu {
    double w;
    string name ;
};
bool  cmp (stu& a, stu& b) {
    if (a.w != b.w  ) {
        return a.w < b.w;
    }
    else {
        return a.name <b.name;
    }
}
int main() {
    vector<stu>  student;
    stu  temp ;
    int index;
    while (cin  >> index ) { // 注意 while 处理多个 case
        student.clear();
        for (int i = 0; i < index; i++) {
            cin >> temp.name >> temp.w;
            student.push_back(temp);
        }
        // sort
        sort (student .begin() ,student.end() ,cmp );

        //cout
        for (int n = 0; n < index ; n++) {
            if (n > 0) cout << ' ';
            cout << student[n].name;
        }
        cout << '\n';
    }

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

03-31 21:47
东南大学 C++
彭于晏前来求offe...:吓晕了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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