#include <stdio.h>
// dp[n] : 정수 n을 1로 만드는 데 필요한 최소 연산 횟수
// 전역 배열이라 자동으로 0으로 초기화됨
// 여기서 0은 "아직 계산 안 됨"의 의미로 사용
int dp[1000001];

int top_down(int n) {
    //기저 조건
    if(n==1) return 0;
    //계산이 된 경우
    if(dp[n]!=0) {
        return dp[n];
    }
    //계산이 안됨
    dp[n]=top_down(n-1)+1;

    if(n%2==0) {
        int tmp=top_down(n/2)+1;
        if(dp[n]>tmp) dp[n]=tmp;
    }
    if(n%3==0) {
        int tmp=top_down(n/3)+1;
        if(dp[n]>tmp) dp[n]=tmp;
    }
    return dp[n];
}

int main() {
    int n;
    scanf("%d",&n);
    
    printf("%d",top_down(n));
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: