# 11490 - The Cat Society
## 題解:
使用qsort排列,
排列順序: occupation -> age -> name
例外: apprentices年齡小的優於大的
**Note**: m可能會大於n, 所以最多顯示n個
## Code:
```c=1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _cat{
char name[35], occupation[35];
int age;
}cats[10005];
typedef struct _cat Cat;
int rank(Cat x){
char r[8][20] = {"elder", "nursy", "kitty", "warrior", "apprentice", "medicent", "deputy", "leader"};
for (int i = 0; i < 8; i++)
if(!strcmp(x.occupation, r[i]))
return i;
return 0;
}
int comp(const void* a, const void* b){
Cat x = *(Cat *)a, y = *(Cat *)b;
int r1 = rank(x), r2 = rank(y);
if(r1 == r2){
if(x.age == y.age)
return strcmp(x.name, y.name);
else{
if(r1 == 4) // apprentice
return x.age - y.age;
else
return y.age - x.age;
}
}
else
return r1 - r2;
}
int main(){
int n, m;
while(scanf("%d%d", &n, &m) != EOF){
for (int i = 0; i < n; i++)
scanf("%s%s%d", cats[i].name, cats[i].occupation, &cats[i].age);
qsort(cats, n, sizeof(Cat), comp);
if(m > n)
m = n;
for (int i = 0; i < m; i++)
printf("%s\n", cats[i].name);
}
return 0;
}
```
###### tags: `NTHUOJ`