1 条题解

  • 0
    @ 2021-6-14 23:29:15

    C++ :

    #include<iostream>  
    #include<string>
    #define LL long long  
    using namespace std;  
    int n,k;  
    char a[50];  
    LL num[50][50];  
    LL dp[50][50][10];  
    int main()  
    {  
        cin>>n>>k;  
        cin>>a;  
        for(int i=0;i<n;i++)  
        {  
            num[i][i]=a[i]-'0';  
            dp[i][i][0]=num[i][i];  
            for(int j=i+1;j<n;j++)  
            {  
                num[i][j]=num[i][j-1]*10+(a[j]-'0');  
                dp[i][j][0]=num[i][j];  
            }  
        }  
        for(int i=0; i<n;i++)  
        {  
            for(int j=i+1;j<n;j++)  
            {  
                for(int kk=1;kk<=k;kk++)  
                {  
                    LL maxV=0;  
                    for(int m=i;m<=j;m++)  
                    {  
                        for(int left=0;left<=kk;left++)  
                        {  
                            if(dp[i][m][left]*dp[m+1][j][kk-left-1]>maxV)  
                                maxV=dp[i][m][left]*dp[m+1][j][kk-left-1];  
                        }  
                    }  
                    dp[i][j][kk]=maxV;  
                }  
            }  
        }  
        cout<<dp[0][n-1][k];  
        return 0;  
    }
    

    Pascal :

    var n,k,i,j,l:integer;a:array[0..50] of integer;sum,f:array[0..50,0..50] of int64;ch:char;
    function max(x,y:int64):int64;
     begin
      if x>y then exit(x) else exit(y);
     end;
    procedure init;
     begin
      readln(n,k);
      fillchar(sum,sizeof(sum),0);
      for i:=1 to n do
       begin
        read(ch);
        val(ch,a[i]);
        sum[i,i]:=a[i];
       end;
      fillchar(f,sizeof(f),0);
      for i:=1 to n-1 do
      for j:=i+1 to n do
      sum[i,j]:=sum[i,j-1]*10+a[j];
      for i:=1 to n do
       f[0,i]:=sum[1,i];
     end;
    begin
     init;
     for i:=1 to k do
     for j:=i to n do
     for l:=1 to j do
      f[i,j]:=max(f[i,j],f[i-1,l]*sum[l+1,j]);
     writeln(f[k,n]);
    end.
    
    • 1

    信息

    ID
    204
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者