给出一个整数n,将n分解为至少两个整数之和,使得这些整数的乘积最大化,输出能够获得的最大的乘积。
例如:
2=1+1,输出1;
10=3+3+4,输出36。
import java.util.*; public class Main{ public static int[] res; public static int solu(int n){ if (n <= 1) return 1; if (res[n] != 0) return res[n]; int maxValue = 0; for (int i = 1;i<=n;i++){ maxValue = Math.max(maxValue, i * Main.solu(n-i)); } res[n] = maxValue; return maxValue; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); Main.res = new int[n+1]; if (n == 2){System.out.println(1);} if (n == 3){System.out.println(2);} else{ System.out.println(Main.solu(n)); } } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); if(n <= 3){ System.out.println(n - 1); }else{ int res = 1; while(n > 4){ res *= 3; n -= 3; } System.out.println(res * n); } } }