【ACM】PAT. A1028 List Sorting【排序】

题目链接
题目分析

按不同属性排序输出

解题思路

结构体排序,按要求使用不同参数cmp即可


AC程序(C++)
/**************************
//@Author: 3stone
//@ACM: PAT-A1028 List Sorting
//@Time: 18/1/27
//@IDE: VS2017
***************************/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>

using namespace std;

typedef struct {
    string id;
    string name;
    int grades;
}Stu;

bool cmp1(Stu s1, Stu s2) {
    return s1.id < s2.id;
}

bool cmp2(Stu s1, Stu s2) {
    if (s1.name != s2.name) return s1.name < s2.name;
    else return s1.id < s2.id;
}

bool cmp3(Stu s1, Stu s2) {
    if (s1.grades != s2.grades) return s1.grades < s2.grades;
    else return s1.id < s2.id;
}

void mySort(Stu stu[], int n, int key) {
    switch (key) {
    case 1:
        sort(stu, stu + n, cmp1);
        break;
    case 2:
        sort(stu, stu + n, cmp2);
        break;
    case 3:
        sort(stu, stu + n, cmp3);
    }
}

Stu stu[100010];

int main() {

    int n, key;
    char id[10], name[10];

    scanf("%d%d", &n, &key);
    for (int i = 0; i < n; i++) {
        scanf("%s %s %d", id, name, &stu[i].grades);
        stu[i].id = id;
        stu[i].name = name;
    }

    mySort(stu, n, key); //按要求排序

    for (int i = 0; i < n; i++) {
        printf("%s %s %d\n", stu[i].id.c_str(), stu[i].name.c_str(), stu[i].grades);
    }

    system("pause");
    return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值