This repository was archived by the owner on Feb 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.c
More file actions
57 lines (45 loc) · 1.39 KB
/
solution.c
File metadata and controls
57 lines (45 loc) · 1.39 KB
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
/* 成绩统计 for problem 242 on XDOJ by LyCecilion - Pure C version */
#include <stdio.h>
#include <stdlib.h>
enum
{
MAX_STUDENTS = 100,
NAME_LEN = 11 /* 10 chars + '\0' */
};
typedef struct
{
int stu_id;
char name[NAME_LEN];
int course_1, course_2, course_3;
double average_score;
} student;
static int cmp(const void *a, const void *b)
{
const student *sa = (const student *)a;
const student *sb = (const student *)b;
if (sa->average_score > sb->average_score)
return -1; /* higher average first */
else if (sa->average_score < sb->average_score)
return 1;
/* smaller id first */
return (sa->stu_id > sb->stu_id) - (sa->stu_id < sb->stu_id);
}
int main(void)
{
int n;
if (scanf("%d", &n) != 1 || n <= 0 || n > MAX_STUDENTS)
return 0;
student stu[MAX_STUDENTS];
for (int i = 0; i < n; ++i)
{
if (scanf("%d %10s %d %d %d", &stu[i].stu_id, stu[i].name, &stu[i].course_1, &stu[i].course_2,
&stu[i].course_3) != 5)
return 0;
const int sum = stu[i].course_1 + stu[i].course_2 + stu[i].course_3;
stu[i].average_score = sum / 3.0;
}
qsort(stu, (size_t)n, sizeof(stu[0]), cmp);
for (int i = 0; i < n; ++i)
printf("%d %s %.1f\n", stu[i].stu_id, stu[i].name, stu[i].average_score);
return 0;
}