c-moodle/1/5.c

38 lines
1.1 KiB
C

#include <stdio.h>
struct student {
int id, c;
float x, y, z;
float sum;
} stu[3];
void swap(struct student* a, struct student* b) {
struct student c;
c = *a;
*a = *b;
*b = c;
}
void sort() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 - i - 1; j++) {
if (stu[j].c > stu[j + 1].c || (stu[j].c == stu[j + 1].c && stu[j].sum < stu[j + 1].sum)) {
swap(&stu[j], &stu[j + 1]);
}
}
}
}
int main() {
scanf("%d,%d,%f,%f,%f", &stu[0].c, &stu[0].id, &stu[0].x, &stu[0].y, &stu[0].z);
scanf("%d,%d,%f,%f,%f", &stu[1].c, &stu[1].id, &stu[1].x, &stu[1].y, &stu[1].z);
scanf("%d,%d,%f,%f,%f", &stu[2].c, &stu[2].id, &stu[2].x, &stu[2].y, &stu[2].z);
stu[0].sum = stu[0].x + stu[0].y + stu[0].z;
stu[1].sum = stu[1].x + stu[1].y + stu[1].z;
stu[2].sum = stu[2].x + stu[2].y + stu[2].z;
sort();
printf("%d,%d,%.1f\n", stu[0].c, stu[0].id, stu[0].sum);
printf("%d,%d,%.1f\n", stu[1].c, stu[1].id, stu[1].sum);
printf("%d,%d,%.1f\n", stu[2].c, stu[2].id, stu[2].sum);
return 0;
}