joj2016: Sort the Students

 2016: Sort the Students


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K1417539Standard

Given the class numbers and the scores of a group of students, you are to write a program to sort the students first by class number in ascending order and then by score in descending order.

Input

The input contains only one test case. The first line of the input contains an integerN(N<=300), indicating the number of students you are to sort. From the second line of the input, there are N lines, each of which contains two integers Ai and Bi, indicating the class number and the score of the ith student.

Output

The output should contain N lines exactly, which should be sorted first by class number in ascending order and then by score in descending order. Each line of the output should contain two integers Aj and Bj(separated by a space) of the student in position j after sorting.

There shoud be no other information or blank in the output. See the sample output below for more details.

Sample Input

5
2 90
2 89
1 65
3 95
2 95

Sample Output

1 65
2 95
2 90
2 89
3 95

 

Problem Source: 2nd JLU Programming Contest

 


This problem is used for contest: 6 


Submit / Problem List / Status / Discuss

 

没考虑自己实现排序,直接调用qsort...

 1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef struct st
5 {
6 int sid;
7 int data;
8 }ST, *PST;
9
10 int cmp(const void *x, const void *y)
11 {
12 PST a = (PST)x;
13 PST b = (PST)y;
14
15 if (a->sid != b->sid)
16 {
17 return a->sid - b->sid;
18 }
19 else
20 {
21 return b->data-a->data;
22 }
23 }
24
25 int main(void)
26 {
27 freopen("in.txt", "r", stdin);
28
29 int n;
30 ST st[350];
31 PST pst = &st[0];
32
33 scanf("%d", &n);
34
35 for (int i=0; i<n; i++)
36 {
37 scanf("%d%d", &((pst+i)->sid), &((pst+i)->data));
38 }
39
40 qsort(st, n, sizeof(st[0]), cmp);
41
42 for (int i=0; i<n; i++)
43 {
44 printf("%d %d", ((pst+i)->sid), ((pst+i)->data));
45 printf("\n");
46 }
47
48 return 0;
49 }



 

转载于:https://www.cnblogs.com/RootJie/archive/2012/02/16/2354705.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值