Untitled

                Never    
C
       
//qsort 裡的 cmp 的參數要用 const void*, 進去 cmp 後再設兩個參數, 轉換成要比較的陣列型別//
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct cat{
    char name[32];
    int occupation;
    int age;
}Cat;

int minimum(int a, int b){
    if(a > b) return b;
    else return a;
}

Cat c[10005];
char table[8][12] = {"elder", "nursy", "kitty", "warrior", "apprentice", "medicent", "deputy", "leader"};

int cmp(const void* cata, const void* catb){
    const Cat* a = (const Cat*)cata;
    const Cat* b = (const Cat*)catb;
    if(a->occupation > b->occupation){
        return 1;
    }
    else if(a->occupation < b->occupation){
        return -1;
    }
    else{
        if((a->occupation == 4) && (b->occupation == 4)){
            if(a->age > b->age){
                return 1;
            }
            else if(a->age < b->age){
                return -1;
            }
            else{
                return strcmp(a->name, b->name);
            }
        }
        else{
            if(a->age < b->age){
                return 1;
            }
            else if(a->age > b->age){
                return -1;
            }
            else{
                return strcmp(a->name, b->name);
            }
        }
    }
}

int main (){
    //create variables//
    int n;
    int m;
    char temp[12];
    while(scanf("%d %d", &n, &m) != EOF){
    //read in data//
        for(int i = 0; i < n; ++i){
            scanf(" %s %s %d", c[i].name, temp, &c[i].age);
        //change occupation into interger//
            for(int j = 0; j < 8; ++j){
                if(strcmp(temp, table[j]) == 0){
                    c[i].occupation = j;
                    break;
                }
            }
        }
    //sort the eating order//
        qsort(c, n, sizeof(Cat), cmp);
    //check if # food greater than # cat//    
        int x = minimum(m, n);
        for(int i = 0; i < x; ++i){
            printf("%s\n", c[i].name);
        }
    }
    return 0;
}
/*
3 3
Dovekit kitty 3
Mousefur elder 40
Graystripe warrior 36
5 3
Firestar leader 36
Dovekit kitty 3
Leafpool warrior 20
Mousefur elder 40
Graystripe warrior 36
5 4
Firestar leader 36
Dovekit apprentice 20
Leafpool apprentice 20
Mousefur warrior 36
Graystripe warrior 36
*/

Raw Text