입력으로 N이 주어진다. 이 때 3과 5를 최소한으로 몇 번 써야 N을 구할 수 있는지 알아내는 문제이다.
출력으로는 사용된 3의 개수와 5의 개수를 각각 출력한다.
만약 구할 수 없으면 -1을 출력한다.
// 아래의 코드는 dev c++환경에서 동작됩니다.
// visual studio에서는 bit/stdc++.h가 기본적으로 없기 때문에
// 정삭적인 작동이 되지 않을 수 있습니다.
#include <bits/stdc++.h>
using namespace std;
int dp[5001];
int main() {
int n;
cin >> n;
dp[3] = dp[5] = 1;
for (int i = 6; i <= n; i++) {
if (dp[i - 3]) dp[i] = dp[i - 3] + 1;
if (dp[i - 5]) dp[i] = dp[i] ? min(dp[i] , dp[i - 5] + 1) : dp[i - 5] + 1;
}
cout << (dp[n] == 0 ? -1 : dp[n]) << endl;
return 0;
}
// 아래의 코드는 dev c++환경에서 동작됩니다.
// visual studio에서는 bit/stdc++.h가 기본적으로 없기 때문에
// 정삭적인 작동이 되지 않을 수 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n, ret;
int main() {
cin >> n;
while (n>=0) {
if (n % 5 == 0) {
ret += (n / 5);
cout << ret << endl;
return 0;
}
n -= 3;
ret++;
}
cout << -1 << endl;
}