题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1

#include <iostream>
#include <variant>
#include<cstdio>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
class Person {
  public:
    Person(string name, int age, int pos) {
        this->name = name;
        this->age = age;
        this->num = pos;
    }
    string name;
    int age;
    int num;
};
class Mycompare {
  public:
    bool operator()(const Person& val1, const Person& val2) {
        if (val1.age != val2.age)
            return val1.age > val2.age;
        else return val1.num > val2.num;
    }
};
class Myless {
  public:
    bool operator()(const Person& val1, const Person& val2) {
        if (val1.age != val2.age)
            return val1.age < val2.age;
        else return val1.num > val2.num;
    }
};
void MyPrint(const vector<Person>& v) {
    for (vector<Person>::const_iterator it = v.begin(); it != v.end(); it++) {
        cout << (*it).name << " " << (*it).age << endl;
    }
}
void test01(int num, int select) {
    // int num=0;
    // cin>>num;
    // bool select;
    // cin>>select;
    vector<Person> v;
    for (; num > 0; num--) {
        string name;
        int age;
        cin >> name;
        cin >> age;
        Person p = Person(name, age,num);
        v.push_back(p);
    }
    if (select) {
        sort(v.begin(), v.end(), Myless());
    } else {
        sort(v.begin(), v.end(), Mycompare());
    }
    MyPrint(v);
}

int main() {
    int num;
    int select;
    while (scanf("%d%d", &num, &select) != EOF) {
        test01(num, select);
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

被加薪的哈里很优秀:应该继续招人,不会给你留岗位的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务