zoukankan      html  css  js  c++  java
  • 1028 List Sorting (25 分)

    #include<iostream>
    #include<string>
    
    using namespace std;
    
    const int N = 100010;
    
    struct student{
        string id, name;
        int grade;
        
        bool operator!=(const student &s) const{
            return id != s.id;
        }
        
    }s[N];
    
    int n, c;
    
    int cmp(student &a, student &b, int c){
        if(c == 2 && a.name != b.name) return a.name < b.name;
        if(c == 3 && a.grade != b.grade) return a.grade < b.grade;
        return a.id < b.id;
    }
    
    void quick_sort(int l, int r, int c){
        if(l >= r) return;
        
        int i = l - 1, j = r + 1;
        student x = s[l + r >> 1];
        
        while(i < j){
            do i ++; while(cmp(s[i], x, c));
            do j --; while(!cmp(s[j], x, c) && s[j] != x);
            if(i < j) swap(s[i], s[j]);
        }
        
        quick_sort(l, j, c);
        quick_sort(j + 1, r, c);
    }
    
    int main(){
        cin >> n >> c;
        
        for(int i = 0; i < n; i ++){
            string id, name;
            int grade;
            
            cin >> id >> name >> grade;
            s[i] = {id, name, grade};
        }
    
        
        quick_sort(0, n - 1, c);
        
        for(int i = 0; i < n; i ++)
            cout << s[i].id << ' ' << s[i].name << ' ' << s[i].grade << endl;
            
        return 0;
    }
    
  • 相关阅读:
    linux-文件
    字符串函数
    函数
    内存管理
    静态库、动态库文件制作
    Makefile 待完善
    指针
    开发板GEC6816环境搭建,使用VS code
    C语言数组
    连接开发板下载程序
  • 原文地址:https://www.cnblogs.com/tomori/p/14961328.html
Copyright © 2011-2022 走看看