...
unknown
plain_text
2 years ago
1.6 kB
12
Indexable
#include <stdio.h> #include <string.h> #include <stdlib.h> struct students { char name[11]; int chinese; int english; int math; int society; int science; int total; }; int cmp(const void *a, const void *b) { struct students *a1 = (struct students *)a; struct students *b1 = (struct students *)b; if (a1->total == b1->total) { if (a1->chinese != b1->chinese) return b1->chinese - a1->chinese; else if (a1->english != b1->english) return b1->english - a1->english; else if (a1->math != b1->math) return b1->math - a1->math; else if (a1->society != b1->society) return b1->society - a1->society; else if (a1->science != b1->science) return b1->science - a1->science; else return strcmp(a1->name, b1->name); } return b1->total - a1->total; } int main() { int n; scanf("%d", &n); struct students *rank = (struct students *)malloc(n * sizeof(struct students)); for (int i = 0; i < n; i++) { scanf("%s %d %d %d %d %d", rank[i].name, &rank[i].chinese, &rank[i].english, &rank[i].math, &rank[i].society, &rank[i].science); rank[i].total = rank[i].chinese + rank[i].english + rank[i].math + rank[i].society + rank[i].science; } qsort(rank, n, sizeof(struct students), cmp); for (int i = 0; i < n; i++) { printf("%s\n", rank[i].name); } free(rank); return 0; }
Editor is loading...
Leave a Comment