-
gori프로그래밍/자료구조 & 알고리즘 2017. 3. 22. 10:36
#include <iostream>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <memory.h>
#include <string>
#include <cmath>
#include <map>
#include <stdlib.h>
using namespace std;
int cache[1000001];
int calc(int n){
if (cache[n] != -1)
return cache[n];
if (n == 1)
return 0;
int min, t;
min = calc(n - 1);
if (n % 3 == 0){
t = calc(n / 3);
if (t < min)
min = t;
}
if (n % 2 == 0){
t = calc(n / 2);
if (t < min)
min = t;
}
return cache[n] = min + 1;
}
int main()
{
int n;
cin >> n;
memset(cache, -1, sizeof(cache));
cout << calc(n);
}