zoukankan      html  css  js  c++  java
  • PAT 1028. List Sorting

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    
    using namespace std;
    
    class Stu {
    public:
        char id[8];
        char name[10];
        char grade;
    };
    
    bool cmp_id(const Stu* a, const Stu* b) {
        return strcmp(a->id, b->id) < 0;
    }
    
    bool cmp_name(const Stu* a, const Stu* b) {
        int res = strcmp(a->name, b->name);
        if (res > 0) {
            return false;
        } else if (res < 0) {
            return true;
        }
        return cmp_id(a, b);
    }
    
    bool cmp_grade(const Stu* a, const Stu* b) {
        if (a->grade > b->grade) {
            return false;
        } else if (a->grade < b->grade) {
            return true;
        }
        return cmp_id(a, b);
    }
    
    int main() {
        int N, C;
        scanf("%d%d", &N, &C);
        vector<Stu*> stus(N, NULL);
    
        for (int i=0; i<N; i++) {
            Stu* cur = stus[i] = new Stu();
            scanf("%s%s%d", cur->id, cur->name, &(cur->grade));
        }
        
        // TODO: use function ptr array
        if (C==1) {
            sort(stus.begin(), stus.end(), cmp_id);
        } else if (C==2) {
            sort(stus.begin(), stus.end(), cmp_name);
        } else {
            sort(stus.begin(), stus.end(), cmp_grade);
        }
        
        for (int i=0; i<N; i++) {
            printf("%s %s %d
    ", stus[i]->id, stus[i]->name, stus[i]->grade);
        }
        return 0;
    }

    还是快不起来,那么水的题

  • 相关阅读:
    RedHat的定制安装
    Linux系统概述
    嵌入式学习方法
    mysql联合查询
    mysql之count
    memcached安装
    css书写规则
    nginx的fastcgi_param参数详解
    array_2.array_rand
    array_1.array_map
  • 原文地址:https://www.cnblogs.com/lailailai/p/4093918.html
Copyright © 2011-2022 走看看