# 12847 - Small Cat Society >author: Utin ###### tags: `qsort` --- ## Brief See the code below ## Solution 0 ```c= #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct _cat { char name[31]; int occu; int age; } Cat; int n, m; Cat arr[10001]; int cmp(const void* a, const void* b); int main() { while (scanf("%d %d", &n, &m) == 2) { for (int i = 0; i < n; i++) { char occu[11]; scanf("%s %s %d", arr[i].name, occu, &arr[i].age); if (occu[0] == 'e') arr[i].occu = 0; else if (occu[0] == 'k') arr[i].occu = 1; else if (occu[0] == 'a') arr[i].occu = 2; } qsort(arr, n, sizeof(Cat), cmp); for (int i = 0; i < n && i < m; i++) { printf("%s\n", arr[i].name); } } } int cmp(const void* a, const void* b) { Cat A = *(Cat*) a; Cat B = *(Cat*) b; if (A.occu != B.occu) return A.occu - B.occu; if (A.age != B.age && A.occu == 2) return A.age - B.age; if (A.age != B.age) return B.age - A.age; return strcmp(A.name, B.name); } // By Utin ``` ## Reference