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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include <stdio.h> #include <string.h> #include <stdlib.h>
typedef struct { int num; int morality; int possibility; int sumOfGrade; } Student;
void initStudents(Student **students, size_t size) { *students = malloc(sizeof(Student) * size); memset(*students, 0, sizeof(Student) * size); }
void setAttribute(Student *student, int num, int morality, int possibility) { student->num = num; student->morality = morality; student->possibility = possibility; student->sumOfGrade = student->morality + student->possibility; }
void printStudents(Student *students, int size) { for (int i = 0; i < size; ++i) { printf("%d %d %d\n", students[i].num, students[i].morality, students[i].possibility); } }
int cmp(const void *student1, const void *student2) { Student *first = (Student *)student1; Student *second = (Student *)student2;
if (first->sumOfGrade != second->sumOfGrade) { return second->sumOfGrade - first->sumOfGrade; } else if (first->morality != second->morality) { return second->morality - first->morality; } else { return first->num - second->num; } }
int main(void) { int n, l, h; int num, morality, possibility; int size1 = 0, size2 = 0, size3 = 0, size4 = 0; Student *students1, *students2, *students3, *students4;
scanf("%d%d%d", &n, &l, &h); initStudents(&students1, n); initStudents(&students2, n); initStudents(&students3, n); initStudents(&students4, n);
for (int i = 0; i < n; ++i) { scanf("%d%d%d", &num, &morality, &possibility); if (morality < l || possibility < l) { continue; } if (morality >= h && possibility >= h) { setAttribute(students1 + size1++, num, morality, possibility); } else if (morality >= h) { setAttribute(students2 + size2++, num, morality, possibility); } else if (morality >= possibility) { setAttribute(students3 + size3++, num, morality, possibility); } else { setAttribute(students4 + size4++, num, morality, possibility); } }
qsort(students1, size1, sizeof(Student), cmp); qsort(students2, size2, sizeof(Student), cmp); qsort(students3, size3, sizeof(Student), cmp); qsort(students4, size4, sizeof(Student), cmp);
printf("%d\n", size1 + size2 + size3 + size4); printStudents(students1, size1); printStudents(students2, size2); printStudents(students3, size3); printStudents(students4, size4);
return 0; }
|