Rohyoohyun / C

0 stars 0 forks source link

10814 나이순정렬 #48

Open Rohyoohyun opened 2 months ago

Rohyoohyun commented 2 months ago

include

include

include

typedef struct { int age; char name[101]; } Person;

int compare(const void a, const void b) { Person A = (Person )a; Person B = (Person )b; return A->age - B->age; }

int main() { int n; scanf("%d", &n);

Person *people = (Person *)malloc(n * sizeof(Person));
for(int i = 0; i < n; i++){
    scanf("%d", &people[i].age);
    getchar(); 
    fgets(people[i].name, 101, stdin);

    size_t len = strlen(people[i].name);
    if (len > 0 && people[i].name[len-1] == '\n') {
        people[i].name[len-1] = '\0';
    }
}

qsort(people, n, sizeof(Person), compare);

for(int i = 0; i < n; i++, puts(""))
    printf("%d %s", people[i].age, people[i].name);

free(people);
return 0;

}