九度-题目1196:成绩排序

http://ac.jobdu.com/problem.php?pid=1196
题目描述:

用一维数组存储学号和成绩,然后,按成绩排序输出。

输入:

输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。

输出:

按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
如果学生的成绩相同,则按照学号的大小进行从小到大排序。

样例输入:
3
1 90
2 87
3 92
样例输出:
2 87
1 90
3 92
来源:
2009年华中科技大学计算机研究生机试真题
排序用内置函数sort(),在结构体中定义排序的规则。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 struct Student{
 8     int credit; //学号
 9     int score;  //成绩
10     bool operator < (const Student &S)const
11     {
12         if(score!=S.score)
13             return score<S.score;
14         else
15             return credit<S.credit;
16     }
17 }list[101];
18 
19 
20 int main()
21 {
22     int n;  //学生个数
23     while(scanf("%d", &n)!=EOF)
24     {
25         for(int i=0; i<n; i++)
26         {
27             scanf("%d %d", &list[i].credit, &list[i].score);
28         }
29 
30         sort(list, list+n);
31 
32         for(int i=0; i<n; i++)
33             printf("%d %d\n", list[i].credit, list[i].score);
34     }
35     return 0;
36 }

 

posted @ 2017-04-24 10:53  悟空的爸爸  阅读(177)  评论(0编辑  收藏  举报