哪位巨佬帮我康康哪里错了啊
查看原帖
哪位巨佬帮我康康哪里错了啊
316827
Temperature_automata楼主2020/12/17 12:32

RT,代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=45;
int n,k;
string num[N][N],f[N][N],s;
int a1[N] , b1[N] , c[N] ;

string max(string a, string b) {
	if(a.size() != b.size()) return a.size() < b.size() ? b : a; 
	return a < b ? b : a; 
}

string multiply (string a,string b) {
  memset(c,0,sizeof(c));
  memset(a1,0,sizeof(a1));
  memset(b1,0,sizeof(b1));
  int lena = a.size() ;
  int lenb = b.size() ;
  for ( int i = 0 ; i < lena ; i ++ ) {
    a1[i+1] = a[lena-i-1] - '0' ;
  }
//   for ( int i = 1 ; i <= lena ; i ++ ) {
//     cout << a1[i] ;
//   }
  for ( int i = 0 ; i < lenb ; i ++ ) {
    b1[i+1] = b[lenb-i-1] - '0' ;
  }
//   for ( int i = 1 ; i <= lenb ; i ++ ) {
//     cout << b1[i] ;
//   }
  int lenc = lena + lenb ;
  int z = 0 ;
  for ( int i = 1 ; i <= lena ; i ++ ) {
    for ( int j = 1 ; j <= lenb ; j ++ ) {
      c[i+j-1] += a1[i] * b1[j] ;
    }
  }
  for ( int i = 1 ; i <= lenc ; i ++ ) {
    c[i+1]+=c[i]/10 ;
    c[i]%=10;
  }
  int j = lenc ;
  while(j>1&&c[j]==0) {
    j-- ;
  }
  string res ;
  for ( int i = j ; i >= 1 ; i -- ) {
    res.push_back( c[i] + '0' ) ;
  }
  return res ;
}

int main()
{
  // freopen("multiply.in","r",stdin) ;
	// freopen("multiply.out","w",stdout) ;
	cin>>n>>k>>s;
  for ( int i = n ; i >= 1 ; i -- ) {
    s[i] = s[i-1] ;
  }
  // for ( int i = 1 ; i <= n ; i ++ ) {
  //   cout << s[i] ;
  // }
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			num[i][j] = s.substr (i,j-i+1) ;
	for(int i=1;i<=n;i++)
	{
		f[i][0]=num[1][i];
		for(int j=1;j<=k;j++)
			for(int m=j;m<i;m++)
				f[i][j]=max(f[i][j],multiply(f[m][j-1],num[m+1][i]));
	}
	cout << f[n][k] ;
	// string a = "12" , b = "16" ;
	// cout << multiply(a,b) ;
	//system("pause") ;
    // return 0 ;
}

2020/12/17 12:32
加载中...