/
1028.c
64 lines (55 loc) · 1.42 KB
/
1028.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
int ID;
char name[9];
int grade;
} Student, *pStudent;
/* increasing by ID */
int cmp1(const void *a, const void *b)
{
pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
return s1->ID - s2->ID;
}
/* non decreasing by name, then increasing by ID */
int cmp2(const void *a, const void *b)
{
pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
if (strcmp(s1->name, s2->name))
return strcmp(s1->name, s2->name);
else
return s1->ID - s2->ID;
}
/* non decreasing by grade, then increasing by ID */
int cmp3(const void *a, const void *b)
{
pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
if (s1->grade - s2->grade)
return s1->grade - s2->grade;
else
return s1->ID - s2->ID;
}
int main()
{
int N, C;
Student buffer[100000] = {0};
pStudent students[100000] = {0}, *s = students;
scanf("%d %d", &N, &C);
for (int i = 0; i < N; i++, s++) {
/* use a new struct buffer */
*s = buffer + i;
scanf("%d %s %d", &(*s)->ID, (*s)->name, &(*s)->grade);
}
if (C == 1) /* increasing ID */
qsort(students, N, sizeof(pStudent), cmp1);
else if (C == 2) /* non-decreasing name */
qsort(students, N, sizeof(pStudent), cmp2);
else /* C == 3, non-decreasing grade */
qsort(students, N, sizeof(pStudent), cmp3);
for (int i = 0; i < N; i++)
printf("%06d %s %d\n", students[i]->ID,
students[i]->name,
students[i]->grade);
return 0;
}