https://www.acmicpc.net/problem/10814
Idea
가입한 순서대로 숫자를 입력해주고 비교하며 qsort
를 써주면 됨.
qsort
짱짱맨
Code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _Person {
char name[101];
int idx;
int age;
}Person;
int compare(const void* a, const void* b);
int main(void) {
int N;
scanf("%d", &N);
Person* arr = (Person*)calloc(N + 1, sizeof(Person));
for (int i = 0; i < N; i++) {
scanf("%d", &arr[i].age);
scanf("%s", arr[i].name);
arr[i].idx = i;
}
qsort(arr, N, sizeof(arr[0]), compare);
for (int i = 0; i < N; i++) {
printf("%d %s\n", arr[i].age, arr[i].name);
}
return 0;
}
int compare(const void* a, const void* b) {
Person A = *(Person*)a;
Person B = *(Person*)b;
if (A.age < B.age) {
return -1;
}
else if (A.age == B.age) {
if (A.idx < B.idx) {
return -1;
}
else {
return 1;
}
}
else {
return 1;
}
return 0;
}