天天看點

【王道機試】排序-成績排序-清華大學

OJ:成績排序

描述

用一維數組存儲學号和成績,然後,按成績排序輸出。

輸入描述:

輸入第一行包括一個整數N(1<=N<=100),代表學生的個數。 接下來的N行每行包括兩個整數p和q,分别代表每個學生的學号和成績。

輸出描述:

按照學生的成績從小到大進行排序,并将排序後的學生資訊列印出來。 如果學生的成績相同,則按照學号的大小進行從小到大排序。

示例1

輸入:

3
1 90
2 87
3 92
           

輸出:

2 87
1 90
3 92
           

代碼如下:

#include<iostream>
#include<algorithm>
using namespace std;
struct student{
    int number;
    int score;
};
bool cmp(student a,student b){
    return a.score==b.score ? a.number<b.number : a.score<b.score ;
}
int main(){
    int n;
    cin>>n;
    student students[100];
    for(int i=0;i<n;i++){
        cin>>students[i].number>>students[i].score;
    }
    sort(students,students+n, cmp);
    for(int i=0;i<n;i++){
        printf("%d %d\n",students[i].number,students[i].score);
    }
    return 0;
}