Problem description
The topic is simple, give n numbers, do not change their relative position, in the middle add K multiplication sign and n-k-1 a plus, (parentheses arbitrarily add) to make the final result as large as possible. Because multiplication sign and the plus sign are all N-1, there is a sign between each of the two adjacent numbers. For example:
n=5,k=2,5 numbers are 1, 2, 3, 4, 5, respectively, can be added:
1*2* (3+4+5) =24
1* (2+3) * (4+5) =45
(1*2+3) * (4+5) =45
......
Input format
There are two lines in the input file, and the first behavior is two integers separated by spaces, representing N and K, where (2<=n<=15, 0<=k<=n-1). The second behavior is n a number separated by spaces (each number is between 0 and 9).
Output format
The output file contains only one line with an integer representing the maximum result required
Sample input
5 2
1 2 3) 4 5
Sample output
120
Sample Description
(1+2+3) *4*5=120
Do not understand or need to exchange students can powder me Sina Weibo @ Rei, private messages YO!!
Every question is written thinking efficiency is too low, there is need or really do not understand go to my Weibo private messages I update blog
Package com. Algo;import Java.util.scanner;public class _116 {public static int N, k;public static int [] array;public static int [] s Um;public static void Main (string[] args) {//TODO auto-generated method Stubscanner scan = new Scanner (system.in); N = Scan.nextint (); K = Scan.nextint (); array = new int[n+1];sum = new Int[n+1];for (int i = 1; I <= N; i++) {Array[i] = Scan.nextint (); Sum[i] = Sum[i-1] + array[i];} int[][] End = new Int[n+1][k+1]; String [] sign = new String[n+1][k+1];for (int i = 1; I <= N; i++) {end[i][0] = sum[i];} for (int i = 1, i <= N; i++) for (int j = 1; J <= K; j + +) {if (i-1 >= j) for (int K = 1; K < i; k++) {if (end[i][j]< end[k][j-1]* (Sum[i]-sum[k])) {end[i][j]=end[k][j-1]* (sum[i]-sum[k]); Sign[i][j] = k + "-" + (j-1);};} /*for (int i = 0; I <= N; i++) {for (int j = 0; J <= K; j + +) System.out.print (end[i][j]+ ""); System.out.println ();} System.out.println ("-----------------"), for (int i = 0, I <= N; i++) {for (int j = 0; J <= K; j + +) System.out.print (sign I [j]+" "); System.out.println ();} */system.out.println (End[n][k]);}}
Blue Bridge Cup algorithm training maximum formula (dynamic programming)