题解 | #成绩排序#
成绩排序
http://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Student {
int number;
int score;
};
const int MaxSize = 100 + 10;
Student arr[MaxSize];
bool Compare(Student x , Student y){
if(x.score == y.score)
return x.number < y.number;
else
return x.score < y.score;
}
int main(){
int n = 0;
//while(scanf("%d",&n)!=EOF)
scanf("%d",&n);
for(int i = 0; i<n;i++){
scanf("%d%d",&arr[i].number,&arr[i].score);
}
sort(arr, arr + n , Compare);
for(int i = 0; i<n ;i++){
cout << arr[i].number << " " << arr[i].score << endl;
}
return 0;
}
//直接从结构体定义中定义大小,此为默认比较方法,不需要compare函数。
struct Student {
int number;
int score;
bool operator< (Student student) const {
if (score == student.score){
return number < student.number;
}else {
return score < student.score;
}
}
}