Total time limit: 1000ms Memory Limit: 65536kB
description
A given n number of 1-9, m plus requirements placed between the digital number (plus sides must have a number), such that the value obtained by adding the minimum expression, and outputs the value. For example, placed in a 1234 Plus, the best method is to put 12 + 34, and 36
Entry
Not more than 15 sets of data
each set of two rows of data. The first row is an integer of m, with m plus sign indicates to put a (0 <= m <= 50 )
the second row is a number of digits. Total number n is not more than 50, and m <= n-1
Export
Each set of data values, the minimum output of the adder expression
Sample input
2 123456 1 123456 4 12345
Sample Output
102 579 15
prompt
To use high-precision calculations, i.e. a large array to store long long integers are fit in, and with addition of large integer analog way of vertical columns.
Problem-solving ideas
The main difficulty in high-precision computing, here are two solutions, the code for future research.
AC code is a
#include <the iostream> #include < String > #include <CString> the using namespace STD; struct the BigInt { int NUM [ 110 ]; int len; the BigInt operator + ( const the BigInt & n-) { // overloaded +, such that a + b when a, b are variables can be established BigInt int ml = max (len, n.len); int with carry = 0 ; // carry BigInt Result; for ( int I = 0 ; I <ml; ++i) { result.num[i] = num[i] + n.num[i] + carry; if( result.num[i] >= 10) { carry = 1; result.num[i] -= 10; } else carry = 0; } if ( carry == 1) { result.len = ml + 1; result.num[ml] = 1; } else result.len = ml; return result; } bool operator<(const BigInt & n) { if( len > n.len ) return false; else if( len < n.len) return true; else { for(int i = len -1; i >= 0; -- i) { if( num[i] < n.num[i]) return true; else IF (NUM [I]> n.num [I]) return to false ; } return to false ; } } the BigInt () { len = . 1 ; Memset (NUM, 0 , the sizeof (NUM)); } the BigInt ( const char * n- , int L) { // a char length L of the large integer array configuration. n ranges from inside the elements 1-9. Memset (NUM, 0 , the sizeof (NUM)); len = L; for ( int i = 0; n[i]; ++i) num[len-i-1] = n[i] - '0'; } }; ostream & operator <<(ostream & o,const BigInt & n) { for(int i = n.len - 1;i >= 0; --i) o << n.num[i]; return o; } const int MAXN = 60; char a[MAXN]; BigInt Num[MAXN][MAXN];//NUM [i] [j] represents an integer of from i-th to j-th digit figures made of the BigInt V [MAXN] [MAXN]; // V [i] [j] denotes the i-th plus sign placed before the j digital intermediate values can get optimal expression. int main () { int m, n-; the BigInt INF; // infinite inf.num [MAXN- 2 ] = . 1 ; inf.len = MAXN- . 1 ; the while (CIN >> m) { CIN >> A + . 1 ; n- strlen = (A + . 1 ); for ( int I = . 1 ; I <= n-; ++ I) for ( int j = i;j<= n; ++j) { Num[i][j] = BigInt(a+i,j-i+1); } for(int j = 1; j <= n; ++j) { V[0][j] = BigInt(a+1,j); } for(int i = 1;i <= m; ++i) { for(int j = 1; j <= n; ++j) { if( j - 1 < i) V[i][j] = inf; else { BigInt tmpMin = inf; for(int k = i; k < j; ++k) { BigInt tmp = V[i-1][k] + Num[k+1][j]; if (tmp < tmpMin) tmpMin = tmp; } V[i][j] = tmpMin; } } } cout << V[m][n] << endl; } return 0; }
AC two codes
#include <iostream> #include <string> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> #include <queue> #include <set> #include <map> #include <list> #include <vector> #include <stack> #define mp make_pair //#define P make_pair #define MIN(a,b) (a>b?b:a) //#define MAX(a,b) (a>b?a:b) typedef long long ll; typedef unsigned long long ull; const int MAX=1e2+5; const int INF=1e8+5; using namespace std; //const int MOD=1e9+7; typedef pair<ll,int> pii; const double eps=0.00000001; string add(string x,string y) { string re; int jin=0; for(int i=x.length()-1,j=y.length()-1;i>=0||j>=0;i--,j--) { re=" "+re; re[0]=(i>=0?x[i]-'0':0)+(j>=0?y[j]-'0':0)+jin; if(re[0]>=10) jin=1,re[0]=(re[0]%10)+'0'; else jin=0,re[0]=re[0]+'0'; } if(jin) re='1'+re; return re; } string mins(string x,string y) { if(x.length()<y.length()) return x; else if(y.length()<x.length()) return y; else return x<y?x:y; } int m; string x; string dp[55][55]; int main() { while(~scanf("%d",&m)) { cin>>x; int len=x.length(); x=" "+x; for(int i=0;i<=len;i++) dp[i][0]=x.substr(1,i); for(int j=1;j<=m;j++) for(int i=j+1;i<=len;i++) for(int s=j;s<i;s++) { if(s==j) dp[i][j]=add(dp[s][j-1],x.substr(s+1,i-s)); else dp[i][j]=mins(dp[i][j],add(dp[s][j-1],x.substr(s+1,i-s))); } cout<<dp[len][m]<<"\n"; } }
Reference Site
https://blog.csdn.net/qq_43472263/article/details/88652211
https://www.cnblogs.com/huashanqingzhu/p/8097787.html