C language using DP Dynamic programming idea to solve maximum K product and product maximum problem _c language

Source: Internet
Author: User

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.
Requirement 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)
Demand output:
The maximum K product calculated.

The idea of solving problems: DP
Set W (h,k): From the 1th to the K-bit of the composition of the decimal number, set M (i,j) to represent the first I (1-i) into the maximum product of J-section, you can get the following classical DP equation:

if (j==1) m (i,j) = W (1,i);

Where: 1<=d< I (that is, starting from 1 to the i-1 to find the maximum value

else if (i < J) m (i,j) = 0; 

code example:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN Wuyi #define MAXK Long m[maxk ][maxn]={{0,0}};
 
/* Initialization operation/Long w[maxn][maxn]={{0,0}};
  void maxdp (int n,int k,int *a) {int i,j,d,h,q,t,s;
  Long Temp,max;
  
 
 
  for (i=1; i<= n; i++)/* divided into 1 paragraphs * * M[I][1] = w[1][i];
 
    for (i=1; i<= n; i++)/* DP process/for (j=2; j<= K; j +) {max = 0;
    for (d=1 d < i; d++) if ((temp = m[d][j-1]*w[d+1][i]) > max) max = temp;
    
    
  M[I][J] = max;
 int main (void) {int n,k,i,j;
 int a[maxn]={0},la=0;
 char c;
 
 scanf ("%d%d", &n,&k);
 while ((C=getchar ())!= ')/* Read data/{A[++la] = C ' 0 ';
   for (i=1; i<= N; i++) {w[i][i]= a[i];
 for (J=i+1 j<= N; j + +) W[i][j] = w[i][j-1]*10 + a[j];
   }/* for (i=1; i<= N; i++) {for (j=1; j<= N; j + +) printf ("%d", w[i][j]);
 printf ("");

 
 } */MAXDP (N,K,A);
 
 printf ("%ld", M[n][k]); /*system ("PaUse "); */return 0;

 }


Product biggest problem:

(The problem with the Max K product is similar to the DP, but some details should be noted, such as: the number of digits is less than multiplication, 0)

Description Description
This year is the international Mathematical Union to determine the "2000--World Mathematics Year", but also coincides with China's famous mathematician Mr. Hua, 90 anniversary of his birthday. In Mr. Hua's hometown of Jintan, Jiangsu, organized an innovative mathematical intelligence competition activities, one of your good friends XZ also had the honor to participate. During the event, the moderator gave all the participants a title:
With a number string of length n, the player is required to use K-multiplication to divide it into k+1 parts, and to find a method of dividing so that the product of the k+1 part can be the largest.
At the same time, in order to help the player to understand the idea correctly, the moderator also cited one of the following examples:
There is a number string: 312, when the n=3,k=1 will have the following two kinds of sub-law:
(1) 3*12=36
(2) 31*2=62
At this point, the result of meeting the requirements of the topic is: 31*2=62
Now, please help your good friend XZ design a program to get the right answer.
Input format
There are two lines of input for the program:
1. There are 2 natural numbers in the first line n,k (6<=n<=40,1<=k<=6)
2. The second line is a numeric string with a K-degree of N.

Output format
The screen output (the result is displayed on the screen), relative to the input, should output the maximum product (a natural number).

Solution: A typical DP problem
A W (h,q) represents the decimal number of a Q-digit combination starting at H-bit, and M (i,j) represents the maximum number of J products for the first I-digit string, and the initial value is:

M (i,0) = W (1,q);

The equations of motion are shown below:

if (j==0) m (i,j) = W (1,q);
else if (j>0)
m (i,j) = max {m (d,j-1) *w (d+1,i-d)} 

PS: 1 <= D < i

Code:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN Wuyi #define MAXK Long m[maxk ][maxn]={{0,0}};
  /* Initialization operation/long My_10_pow (int t) {long sum=1;
  int y;
  
  for (Y=1 y<= t; y++) sum *= 10;
return sum;
  Long W (int start,int len,int *a)/* Converts the number string to the corresponding decimal number/{Long res = 0;
  int t,f;
  
  for (f=start,t=len-1;t >= 0; f++,t--) res = A[F]*MY_10_POW (t);
return res;
  } void maxdp (int n,int k,int *a) {int i,j,d,h,q,t,s;
  Long Temp,max;
  
 
 
  for (i=1; i<= n; i++) m[i][0] = W (1,i,a); for (i=1; i<= n; i++)/*DP process ....
    * for (j=1; j<= K; j +) {max = 0;
    if (i <= j)/* If the length is less than the number of multiplication, the value is 0*/m[i][j] = 0;
    else {for (d=1 D < i; d++) if ((temp = m[d][j-1]*w (d+1,i-d,a)) > max) max = temp;
    M[I][J] = max;
 int main (void) {int n,k,i,j;
 int a[maxn]={0},la=0;
 char c;
 
 scanf ("%d%d", &n,&k); while ((C=getchar ())!= ')/* Read data/{A[++la] = C ' 0 ';
 
 } MAXDP (N,k,a);
 
 printf ("max =%ld", m[n][k]);
 
 System ("pause");
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.