简介

对cmp的理解能力
常规题

code

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

struct Student{
    int idx;
    int num;
    string name;
    Student(int idx_, int num_, string name_) : idx(idx_), num(num_), name(name_){};
    bool cmp(const Student& b) {
        if(num < b.num) {
            return true;
        }else if(num == b.num){
            return idx < b.idx;
        }else{
            return false;
        }
    }
};
    bool cmp(const Student& a, const Student& b) {
        if(a.num < b.num) {
            return true;
        }else if(a.num == b.num){
            return a.idx < b.idx;
        }else{
            return false;
        }
    }
    bool cmpC(const Student& a, const Student& b) {
        if(a.num > b.num) {
            return true;
        }else if(a.num == b.num){
            return a.idx < b.idx;
        }else{
            return false;
        }
    }
int main () {
    int n;
    int t;
    while(cin >> n) {
        cin >> t;
        int idx = 0;
        int num = 0;
        string name;
        vector<Student> v;
        while(n) {
            cin >> name >> num;
            idx++;
            v.push_back({idx, num, name});
            n--;
        }
        
        
        if(t != 0) {
            sort(v.begin(), v.end(), cmp);
            for(auto it:v){
                cout << it.name << " " << it.num << endl;
            }
        }else{
            sort(v.begin(), v.end(), cmpC);
            for(auto it:v){
                cout << it.name << " " << it.num << endl;
            }
        }
    }
}
posted on 2021-06-28 13:46  HDU李少帅  阅读(50)  评论(0编辑  收藏  举报