당신은 택배회사 사장이다
택배 상자는 2가지 종류가 있다.
5개 짜리 박스, 3개 짜리 박스.
주문이 들어오면 저 두가지 박스에 나눠서 넣는데,
5개 짜리 박스가 가성비가 좋아 최대한 많이 사용해야 한다.
1. 5개 짜리, 3개 짜리 박스로 포장이 불가능한 주문은 -1 리턴
ex) 17
2. 5개 짜리 박스를 최대한 많이 사용
ex) input : 18, output : 3, 1
의식의 흐름대로 짰을 경우 아래와 같다
#include <stdio.h>
void packing(int post) {
int count_5 = 0;
int count_3 = 0;
if (post % 5 == 0 || post % 3 == 0) {
count_5 = post / 5;
count_3 = (post - (count_5 * 5)) / 3;
while (1) {
if ( (post - (count_5 * 5) - (count_3 * 3)) == 0) {
printf("count_5 = %d, count_3 = %d\n", count_5, count_3);
return;
} else {
count_5--;
count_3 = (post - (count_5 * 5)) /3;
}
}
printf("??\n");
} else {
printf("-1\n");
return;
}
}
int main() {
int post = 21;
scanf("%d", &post);
packing(post);
return 0;
}
'Programming > Programmers 알고리즘 문제' 카테고리의 다른 글
structsize (0) | 2019.10.24 |
---|---|
arrayshift (0) | 2019.10.24 |
quickselect (0) | 2019.10.24 |
quicksort (0) | 2019.10.24 |
파이썬 공부 (0) | 2018.04.15 |