twofive 배열의 순서를 2,5 로 하냐, 5,2로 하냐에 차이가 있었다.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <limits.h>
using namespace std;
int twofive[2] = { 5,2 };
int cache[100001];
int n;
int go(int money) {
       cout << money << '\n';
       if (money == 0) {
              return cache[money];
       }
       if (money < 0) {
              return 999999;
       }
       
       int& ret = cache[money];
       if (ret != -1) {
              return ret;
       }
       cache[money] = 999999;
       for (int i = 0; i < 2; ++i) {
              ret = min(ret, go(money - twofive[i]) + 1);
       }
       return ret;
}
int main(void) {
       memset(cache, -1, sizeof(cache));
       cin >> n;
       int ans = go(n) + 1;
       
       cout << (ans > 999990 ? -1 : ans) << endl;
       return 0;
}


'알고리즘' 카테고리의 다른 글

[DP] 백준 1965 상자넣기  (0) 2018.04.02
[EASY] 백준 2167 2차원 배열의 합  (0) 2018.04.02
[DP] 백준 9084 동전  (0) 2018.04.02
[DP] 백준 동전 2 2294  (0) 2018.04.02
[DP] 백준 2011 암호코드  (0) 2018.01.02

+ Recent posts