[Dp] matrix multiplication / multiplication

K product biggest problem

<< Description of the problem

 Let I n is a decimal integer. If I is divided into k segments integer k can be obtained. This product is called an integer k k I is multiplied. Try to design an algorithm for a given k and I, I is the maximum k obtained product.

Decimal integer, for example, is divided into three segments 1234 may have the following situations:

1 × 2 × 34 = 68

1 × 23 × 4 = 92

12 × 3 × 4 = 144

<< programming tasks

    For a given product and I k, I calculated the programmed maximum k.

<< data input

The first input line has two positive integers n and k. N is a positive integer sequence length; k is a positive integer division of the number of segments. The next line is a n-digit decimal integer. (N <= 10)

  << resulting output ss

          K calculated maximum product.

Input File Example

Output File Example

input.txt

output.txt

3 2

312

62

 

        << Tips for Reaching

  Reference Matrix continually multiply problems.

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
/*
void copyMatrixchain(){
    int i,r,j,k;
    memset(m,0,sizeof(m));
    memset(s,0,sizeof(s));
    for(r=2;r<=n;r++)                  //跨度从2-n
    {
        for(i=1;i<=n-r+1;i++)        //遍历
        {
            j=i+r-1;
            m[i][j]=m[i+1][j]+p[i-1]*p[i]*p[j];
            s[i][j]=i;
            for(k=i+1;k<j;k++)          //把矩阵乘法分成两部分,处理前后部分后相乘
            {
                int t=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
                if(t<m[i][j])
                {
                    m[i][j]=t;
                    s[i][j]=k;
                }
            }
        }
    }
}
*/

long m[20][20];
long l[20][20];
FILE *in,*out;
void dp(int n,int k)
{
    int i,j,t;
    long tmax;
    for(i=1;i<=n;i++)
        m[i][1]=l[1][i];    //分成一个数显然是最大的

    for(i=1;i<=n;i++)      //对1-n分成2-k个数
    {
        for(j=2;j<=k;j++)      //分割后数的个数
        {
            tmax=0;
            for(t=1;t<=i;t++)
            {
                if((m[t][j-1]*l[t+1][i])>tmax)      //递推
                    tmax=m[t][j-1]*l[t+1][i];
            }
            m[i][j]=tmax;
        }
    }
        fprintf(out,"%ld",m[n][k]);
}
int main()
{

    int n,k,a,temp,t,i,j;
    int num[10];
    in=fopen("d:\\input.txt","r");
    out=fopen("d:\\output.txt","a");
    if(!in||!out)
        printf("文件打开失败!");

    fscanf(in,"%d%d",&n,&k);
    t=n;
    fscanf(in,"%d",&a);
    temp=a;
    while(t)
    {
        num[t]=temp%10;
        temp/=10;
        t--;
    }
    for(i=1;i<=k;i++)
        printf("%d   ",num[i]);
    for(i=1;i<=n;i++)
        for(j=i;j<=n;j++)
        {

            int t = 0;
            temp=0;
            while(t<j-i+1)
            {

                temp=temp*10+num[i+t];
                t++;
            }
            l[i][j]=temp;
    //        printf("||%d %d:%d||   ",i,j,w[i][j]);
        }

        dp(n,k);
        fclose(in);
        fclose(out);
        return 0;
}

 

Published 28 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41514794/article/details/88604062