1028 List Sorting (25分)

1. 题目

1028 List Sorting (25分)

2. 思路

常规题

3. 注意点

4. 代码

#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream> 

// 14:40 -  14:53
using namespace std;

struct people{
    string id;
    string name;
    int grade;
    people(string id, string name, int grade){
        this->id = id;
        this->name = name;
        this->grade = grade;
    }
};

int n, c;
vector<people> p;

bool cmp1(people a, people b){
    return a.id < b.id;
}

bool cmp2(people a, people b){
    if(a.name != b.name){
        return a.name < b.name;
    }
    return a.id < b.id;
}

bool cmp3(people a, people b){
    if(a.grade != b.grade){
        return a.grade < b.grade;
    }
    return a.id < b.id;
}

void print(vector<people> p1){
    for(auto p:p1){
        printf("%s %s %d\n", p.id.data(), p.name.data(), p.grade);
    }
}

int main(){
    scanf("%d %d", &n, &c);
    char id[100], name[100];
    int g;
    for(int i=0;i<n;i++){
        scanf("%s %s %d", id, name, &g);
        p.push_back(people(id, name, g));
    }
    if(c == 1){
        sort(p.begin(), p.end(), cmp1);
    }else if(c == 2){
        sort(p.begin(), p.end(), cmp2);
    }else{
        sort(p.begin(), p.end(), cmp3);
    }
    print(p);
}
上一篇:[USACO][DAG上的动态规划]Sorting A Three-Valued Sequence


下一篇:PAT A1028 List Sorting (25分)