fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc=new Scanner(System.in);
  14. int n=sc.nextInt();
  15.  
  16. int dp[]=new int[n+1];
  17. for(int i=2;i<=n;i++){
  18. int s1=dp[i-1]+1;
  19. int s2=Integer.MAX_VALUE;
  20. int s3=Integer.MAX_VALUE;
  21. int x=Integer.MAX_VALUE;
  22. if(i%2==0){
  23. s2=dp[i/2]+1;
  24. }
  25. if(i%3==0){
  26. s3=dp[i/3]+1;
  27. }
  28. if(i%2!=0 && i%3!=0){
  29. x=Math.max(dp[i-1],dp[i-2])+1;
  30. }
  31. dp[i]=Math.min(s1,Math.min(s2,Math.min(s3,x)));
  32. }
  33. System.out.println(dp[n]);
  34. }
  35. }
Success #stdin #stdout 0.13s 56600KB
stdin
100
stdout
7