题目:
import java.util.Scanner; public class tass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt(); } int k = sc.nextInt(); int maxArea[][] = new int[n][n];//f[i][j]表示从i到j最大的数,例如f[2][5]表示从下标为2到下标为5的区域内最大的数 for (int j = 0; j < maxArea.length; j++) { maxArea[j][j] = a[j]; } for (int i = 1; i < maxArea.length; i++) { for (int j = 0; j < maxArea.length-i; j++) { maxArea[j][j+i] = Math.max(maxArea[j][j+i-1], maxArea[j+i][j+i]); } } int f[][] = new int[n+1][k+1];//f[i][j]存放前i个数里挑选j个数的最大乘积,即最终要求条件 for (int i = 1; i < f.length; i++) {//求出前i个数里挑选1个的最大乘积 f[i][1] = maxArea[0][i-1]; } for (int w = 2; w <= k; w++) { for (int i = w; i <= maxArea.length; i++) { for (int j = w-1; j < i; j++) { f[i][w] = Math.max(f[i][w], f[j][w-1]*maxArea[j][i-1]); } } } System.out.println(f[n][k]); } }