九度-题目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 }

 

转载于:https://www.cnblogs.com/shenckicc/p/6755744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值