[백준] 2839 설탕 배달
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n;
cin >> n;
if ((n % 5) == 0) {
cout << (n / 5);
return 0;
}
int answer = 987654321;
int cnt = 0;
while (n > 0) {
if ((n % 3) == 0) {
answer = min(answer, cnt + (n / 3));
}
n -= 5;
cnt++;
}
if (answer == 987654321) cout << -1;
else cout << answer;
return 0;
}
3년 전 코드
#include <stdio.h>
int main() {
int N;
scanf("%d", &N);
int i;
int j;
int answer = 5000 / 3;
for (int i = 0; i <= 1000; i++) {
for (int j = 0; j <= 5000 / 3; j++) {
if (N == 5 * i + 3 * j) {
if (answer > i + j)answer = i + j; }
}
}
if (answer != 5000 / 3) printf("%d", answer);
else printf("%d", -1);
return 0;
}