经典算法题之-成绩排序C++

发布时间 2024-01-07 16:58:46作者: 神奇的萝卜丝

sort实在是太好用了。活用sort,一切排序题目都可以秒杀。

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

struct node{
    int num;
    int date;
};
typedef struct node student ;

bool comp(student left,student right){
    if(left.date < right.date) return true;
    if(left.date==right.date){
        if(left.num < right.num) return true;
    }
    return false;
}

int main(){
    student A[101];
    int n = 0 ;
    while( cin >> n ){
        for(int i = 0 ; i < n ; i++){
            cin >> A[i].num >> A[i].date ;
        }
        sort(A,&A[n], comp);
        for(int i = 0 ; i < n ; i++){
            cout << A[i].num << ' ' << A[i].date << '\n';
        }
    }
    return 0;
}
结果如下: