joj2016: Sort the Students

 2016: Sort the Students


Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 1417 539 Standard

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 
2 #include
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; i36 {
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; i43 {
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

你可能感兴趣的:(joj2016: Sort the Students)