Created
February 26, 2018 06:08
-
-
Save yaboong/11c5edb33b89a0191d9882ee177ac553 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* Created by yaboong on 2018. 2. 26.. | |
*/ | |
public class ToOne { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int N = sc.nextInt(); | |
int[] dp = new int[N+1]; | |
dp[0] = -1; | |
dp[1] = 0; | |
for (int n=2; n<=N; n++) { | |
dp[n] = dp[n-1] + 1; | |
if (n % 2 == 0 && dp[n] > dp[n/2] + 1) | |
{ | |
dp[n] = dp[n/2] + 1; | |
} | |
if (n % 3 == 0 && dp[n] > dp[n/3] + 1) | |
{ | |
dp[n] = dp[n/3] + 1; | |
} | |
} | |
System.out.println(dp[N]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment