B. Split a Number (String addition)

Dima worked all day and wrote down on a long paper strip his favorite number nn consisting of ll digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf.

To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip.

Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain.

Input

The first line contains a single integer ll (2l1000002≤l≤100000) — the length of the Dima's favorite number.

The second line contains the positive integer nn initially written on the strip: the Dima's favorite number.

The integer nn consists of exactly ll digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip.

Output

Print a single integer — the smallest number Dima can obtain.

Examples
input
Copy
7
1234567
output
Copy
1801
input
Copy
3
101
output
Copy
11
Note

In the first example Dima can split the number 12345671234567 into integers 12341234 and 567567. Their sum is 18011801.

In the second example Dima can split the number 101101 into integers 1010 and 11. Their sum is 1111. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.

 

 Solution: To separate the two sides from the middle branch, the optimal solution can be taken.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string cal(string a,string b)//字符串加法,模拟数的加法即可
{
    string ans="";
    int pos1=a.size()-1,pos2=b.size()-1;
    int last=0,x=0;
    while(1){
        if(pos1<0&&pos2<0)break;
        if(pos1<0&&pos2>=0){
            while(pos2>=0){
                x=b[pos2--]-'0'+last;
                if(x>=10){
                    last=x/10;
                    x%=10;
                }
                else
                    last=0;
                years + x + = ' 0 ' ;
            }
            break;
        }
        if(pos2<0&&pos1>=0){
            while(pos1>=0){
                x=a[pos1--]-'0'+last;
                if(x>=10){
                    last=x/10;
                    x%=10;
                }
                else
                    last=0;
                years + x + = ' 0 ' ;
            }
            break;
        }
        x=a[pos1--]-'0'+b[pos2--]-'0'+last;
        if(x>=10){
            last=x/10;
            x%=10;
        }
        else
            last=0;
        years + x + = ' 0 ' ;
    }
    if(last)
        years + = last + ' 0 ' ;
    return years;
}
int main ()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int pos1=n/2,pos2=n/2+1;
    while(s[pos1]=='0'&&pos1>0)pos1--;
    while(s[pos2]=='0'&&pos2<n-1)pos2++;

    string a=s.substr(0,pos1);
    string b=s.substr(pos1,s.size());
    string ans=cal(a,b);
    reverse (ans.begin () ans.end ());
    string aa=s.substr(0,pos2);
    string bb=s.substr(pos2,s.size());
    string anss=cal(aa,bb);
    reverse(anss.begin(),anss.end());
    IF (S [pos2] == ' 0 ' ) // when a part of the special sentence can not be divided, if the thought of it, this time the title game will be able to make out the Ya-Ya Ya 
        return cout << endl << ANS, 0 ;
     IF (ans.size () <anss.size ()) COUT ANS << << endl;
     the else  IF (ans.size ()> anss.size ()) COUT ANSS << << endl;
     the else {
         IF ( ANS < ANSS)
            cout<<ans<<endl;
        else
            cout<<anss<<endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/cherish-lin/p/11035894.html