Programming Language/문제
[코드업] 3019 : 스케줄 정리
장영현
2023. 6. 17. 11:17
728x90
https://codeup.kr/problem.php?id=3019&rid=0
스케줄 정리
5 sleep 2014 05 23 golf 2014 06 02 travel 2015 11 22 baseball 2013 02 01 study 2014 05 23
codeup.kr
//버블정렬 사용
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int year;
int month;
int day;
char work[100];
} plan;
int compare(const plan* a, const plan* b) {
if (a->year != b->year)
return a->year - b->year;
if (a->month != b->month)
return a->month - b->month;
if (a->day != b->day)
return a->day - b->day;
return strcmp(a->work, b->work);
}
void bubbleSort(plan* arr, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if (compare(&arr[j], &arr[j + 1]) > 0) {
plan temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int count;
scanf("%d", &count);
plan* plan_name = (plan*)malloc(count * sizeof(plan));
for (int i = 0; i < count; i++) {
scanf("%s %d %d %d", plan_name[i].work, &(plan_name[i].year), &(plan_name[i].month), &(plan_name[i].day));
}
bubbleSort(plan_name, count);
for (int i = 0; i < count; i++) {
printf("%s\n", plan_name[i].work);
}
free(plan_name);
return 0;
}
//퀵정렬 사용
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int year;
int month;
int day;
char work[100];
} plan;
int compare(const void* a, const void* b) {
const plan* plan_a = (const plan*)a;
const plan* plan_b = (const plan*)b;
if (plan_a->year != plan_b->year)
return plan_a->year - plan_b->year;
if (plan_a->month != plan_b->month)
return plan_a->month - plan_b->month;
if (plan_a->day != plan_b->day)
return plan_a->day - plan_b->day;
return strcmp(plan_a->work, plan_b->work);
}
int main() {
int count;
scanf("%d", &count);
plan* plan_name = (plan*)malloc(count * sizeof(plan));
for (int i = 0; i < count; i++) {
scanf("%s %d %d %d", plan_name[i].work, &(plan_name[i].year), &(plan_name[i].month), &(plan_name[i].day));
}
qsort(plan_name, count, sizeof(plan), compare);
for (int i = 0; i < count; i++) {
printf("%s\n", plan_name[i].work);
}
free(plan_name);
return 0;
}
구조체 연습