Max K product Problem:
Set I is an n-bit decimal integer. If I is divided into K, we can get k integers. The product of this K-integer is called a K product of I. Try to design an algorithm, for given I and K, to find the maximum K product of I.
Programming tasks:
For the given I and K, the maximum K product of I is programmed to compute.
Input
There are 2 positive integers n and K in line 1th of the input. The positive integer n is the length of the sequence, and the positive integer k is the number of segments that are divided. The next line is an n-bit decimal integer. (n<=10)
Output
The maximum K product calculated.
Input:
4 R
54321
Output:
6420 1 #include
2 #include
3 #include
4 #define MAXN 51
5 #define MAXK 10
6//m[i][j] represents the maximum product of a 1~i decimal bit divided into J segments
7Longm[maxk][maxn]={{0,0}};
8//w[i][j] Represents a decimal number that consists of a i~j decimal bit
9Longw[maxn][maxn]={{0,0}};
10intFOOT[MAXN][MAXN] = {{0,0}};
11
12voidMAXDP (intNintKint*A)
13 {
14intI,j,d,h,q,t,s;
15LongTemp,max;
16 for(I=1; i<= n; i++)//divided into a paragraph
M[I][1] = W[1][i];
18
19 for(I=1; i<= n; i++)/DP Process * *
20 for(j=2; j<= K. J)
21 {
max = 0;
23 for(D=1 d < i; d++)
24 {
25if((temp = m[d][j-1]*w[d+1][i]) > Max)
26 {
max = temp;
Foot[i][j]=d;
29}
30}
M[I][J] = max;
32}
33}
34
35//Output after the partition of K number
36voidPrint_foot (int*data,intNintK
37 {
38intD, I;
39intSTACK[256];
40inttop = 0;
41inttmp
42
TMP = n;
44 while((TMP = foot[tmp][k]) > 0)
45 {
stack[top++] = tmp;
k--;
48}
printf ("Divided sequence:/n");
i = 1;
51 while((--top) >= 0)
52 {
TMP = Stack[top];
54 for(; I <= tmp; i++)
55 {
printf ("%d", data[i]);
57}
To printf ("");
59}
60 for(; I <= n; i++)
61 {
printf ("%d", data[i]);
63}
printf ("n");
65
66}
67
68
69intMainvoid)
70 {
71intN,k,i,j;
72inta[maxn]={0},la=0;
73CharC
scanf ("%d%d", &n,&k); Input N, k
75 while((C=getchar ())!= '/n ')//read integer
76 {
A[++la] = C ' 0 ';
78}
79 for(I=1; i<= N; i++)
80 {
Bayi w[i][i]= A[i];
82 for(j=i+1; j<= N; j + +)
W[I][J] = w[i][j-1]*10 + a[j];
84}
MAXDP (N,k,a);
To printf ("%ld/n", M[n][k]);
Print_foot (A, n, K);
88 return0;
89}
90