안녕하세요 c++ 공부하고있는 대학생입니다.
이번에는 codeup 이라는 사이트에서 정올 1번수준 문제 리스트 안에있는 4051문제를 풀어보았습니다.
[문제1] 시간외 근무 수당 (16점, 제한시간 1초)
A대학에서는 시간외 근무를 수기로 작성하고 있다. 행정실의 K씨는 시간외 근무 수당을 전산으로 처리하고자 한다. 이 대학의 시간외 근무 수당 규정은 아래와 같다.
시간외 근무는 최초 1시간은 제외하며, 최대 4시간까지 인정한다.
시간외 근무에 따른 금액은 30분에 5,000원으로 한다.
5일간의 시간외 근무 합계가 15시간 이상이면 받는 금액의 5%를 감하여 지급한다.
5일간의 시간외 근무 합계가 5시간 이하이면 받는 금액의 5%를 더하여 지급한다.
시간외 근무 수당을 계산하는 프로그램을 작성하시오.
입력
5일간의 시간외 근무 시작 시각 s와 종료 시각 e가 주어진다. (단, 16.0 ≦ s ≦ e ≦ 24.0)
30분 단위로 입력이 가능하며, 30분은 0.5로 입력된다. (16시 30분은 16.5로 표기한다.)
출력
첫 줄에 최종 시간외 근무 수당을 출력한다(단, 단위는 생략한다).
입력 예시
16.0 16.5
17.0 20.5
18.5 23.0
22.5 23.5
19.5 22.5
출력 예시
80000
제 소스코드를 먼저 보여드리겠습니다.
#include <stdio.h>
#include <stdlib.h>
int result = 0;
int index = 0;
typedef struct node {
float time;
float atTime;
int check;
struct node* next;
}node;
typedef struct List {
struct node* head;
int count;
}List;
List* init() {
List* list = (List*)malloc(sizeof(List));
if (list == NULL) {
printf("err\n");
}
else {
list->head = NULL;
list->count = 0;
return list;
}
}
void insert(List* list, float time, float atTime) {
node* newNode = (node*)malloc(sizeof(node));
node* preNode = list->head;
newNode->time = time;
newNode->atTime = atTime;
newNode->check = 0;
newNode->next = NULL;
if (list->count == 0) {
newNode->next = newNode;
list->head = newNode;
index++;
}
else {
for (int i = 0; i < index; i++) {
preNode = preNode->next;
}
newNode->next = preNode->next;
preNode->next = newNode;
}
list->count++;
}
void check(List* list) {
float timecheck, count;
node* curNode = list->head;
if (list->count == 0) return;
if (list->head->check == 0) {
if (list->head->atTime <= list->head->time + 1) {
list->count--;
list->head = list->head->next;
free(curNode);
check(list);
}
else {
timecheck = list->head->atTime - (list->head->time + 1);
count = timecheck / 0.5;
if (count >= 8) {
result += 40000;
}
else {
result += count * 5000;
}
list->count--;
list->head = list->head->next;
free(curNode);
check(list);
}
}
}
int main() {
List* list = init();
float time, atTime;
for (int i = 0; i < 5; i++) {
float time, atTime;
scanf("%f %f", &time, &atTime);
insert(list, time, atTime);
}
check(list);
if (result >= 150000) {
result = result - (result * 0.05);
}
else if (result <= 50000) {
result = result + (result * 0.05);
}
printf("%d", result);
free(list);
return 0;
}
이 문제는 어려운 문제가 아니기때문에, 초과근무 시간에서 1시간을 제외 와 초과근무 최대시간 4시간만 고려 해 준다면 금방 풀 수 있는 문제입니다.
사용한 자료구조는 연결리스트로 단방향 연결리스트를 사용하였습니다.
연결리스트의 head점을 시작점으로, 순회하면서 체크를 해 주며, 재귀형식으로 계속 check를 해 주었습니다. 체크를 한 후, free로 반환을 해 주었습니다.
codeup 사이트에 4051번에 제출을 하면 정답체크가 뜨는 것 까지 확인하였습니다.