1 条题解

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

    C++ :

    #include<iostream>
    
    #include<string.h>
    
    #include<algorithm>
    
    using namespace std;
    int  max(int a,long b )
    {
     
      if(a>b)
    	  return a;
      else
    	  return b;
    }
    
    int main(){
    
        int n,K,a[45];
    
        char x;
    
        long dp[45][45];
    
        while(cin>>n>>K){
    
            memset(a,0,sizeof(a));
    
            memset(dp,0,sizeof(dp));
    
            for(int i=1;i<=n;i++){
    
                cin>>x;a[i]=x-'0';
    
            }
    
            for(int k=1;k<=K;k++){
    
                    for(int i=k+1;i<=n;i++){
    
                        for(int j=k;j<i;j++){
    
                        int index1=1,num1=0,index2=j+1,num2=0;
    
                        while(index1<=j){
    
                                num1=num1*10+a[index1];index1++;
    
                        }
    
                        while(index2<=i){
    
                            num2=num2*10+a[index2];index2++;
    
                        }
    
                        if(k==1) dp[i][k]=max(dp[i][k],(long)num1*num2);
    
                        else dp[i][k]=max(dp[i][k],dp[j][k-1]*num2);
    
                    }
    
                }
    
            }
    
            cout<<dp[n][K]<<endl;
    
        }
    
        return 0;
    
    }
    

    Pascal :

    const MAXN=40;
    MAXK=6;
    var
    i,j,l:longint;
    n,K:longint;
    s:string;
    f:array[0..MAXN,0..MAXK] of int64;
    a:array[0..MAXN,0..MAXN] of int64;
    begin
    {assign(input,'2000t2.in');reset(input);
    assign(output,'2000t2.out');rewrite(output); }
    readln(N,K);
    readln(s);
    fillchar(f,sizeof(f),0);
    fillchar(a,sizeof(a),0);
    for i:=1 to n do
    begin
    j:=ord(s[i]);
    dec(j,ord('0'));
    a[i,i]:=j;
    end;
    for i:=1 to n do
    for j:=i+1 to n do a[i][j]:=a[i][j-1]*10+a[j][j];
    for i:=1 to n do f[i][0]:=a[1][i];
    for l:=1 to K do
    for i:=l+1 to n do
    begin
    for j:=l-1+1 to i-1 do
    if f[i][l]<f[j][l-1]*a[j+1][i] then f[i][l]:=f[j][l-1]*a[j+1][i];
    end;
    writeln(f[N][K]);
    {close(input);
    close(output);}
    end.
    

    Java :

    import java.math.*;
    import java.util.*;
    public class Main { 
    	public static void main(String[] args) {
    		Scanner cin = new Scanner ( System.in );
    		String str;
    		int n ,k;
    		while(cin.hasNext())
    		{
    		 n=cin.nextInt();
    		 k=cin.nextInt();
    		 str=cin.next();
    		 BigInteger dp[][]=new BigInteger[41][7];
    		 BigInteger num[][]=new BigInteger[41][41];
    		 for(int i=1;i<=n;i++)
    		 {
    			 for(int j=i;j<=n;j++)
    			 {
    				 String s ="";
    				 for(int l=i-1;l<j;l++)
    				 {
    					 s+=str.charAt(l);
    				 }
    				 BigInteger tmp = new BigInteger(s);
    				 num[i][j] = tmp;
    			 }
    		 }
    		 BigInteger t = new BigInteger("0");
    		 for(int i=1;i<=n;i++)
    		 {
    			 for(int j=1;j<=k;j++)
    			 {
    				 dp[i][j]=t;
    			 }
    		 }
    		 String s="";
    		 for(int i=1;i<=n;i++)
    		 {
    			 s+=str.charAt(i-1);
    			 BigInteger tmp = new BigInteger(s);
    			 dp[i][0]=tmp;
    		 }
    		 for(int i=2;i<=n;i++)
    		 {
    			 for(int l=1;l<=k;l++)
    			 {
    				 for(int j=i-1;j>=1;j--)
    				 {
    					 dp[i][l]=dp[i][l].max(dp[j][l-1].multiply(num[j+1][i]));
    				 }
    			 }
    		 }
    		 System.out.println(dp[n][k]);
    		}
    		}
    }
    
    • 1

    信息

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