乐逍遥xwl

导航

Codeforces Round #567 (Div. 2) B. Split a Number

Codeforces Round #567 (Div. 2)

 

B. Split a Number

 

Dima worked all day and wrote down on a long paper strip his favorite number n consisting of l 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 l (2≤l≤100000) — the length of the Dima's favorite number.

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

The integer n consists of exactly l 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
7
1234567
output
1801
input
3
101
output
11

Note

In the first example Dima can split the number 1234567 into integers 1234 and 567. Their sum is 1801.

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

 

题意:题目意思是给你一个长度为n的字符串,让你在中间截一下,变成两个字符串,

然后当做数字求和,找最小值,不过截出的字符串不能有前导0。

思路:显然应该想到,当两个字符串位数相近才能加出最小值,

那么就可以从字符串中间开始截,只不过考虑到有位数的奇偶和0的字符,

所以要从中间往两边延伸截字符串,直到成功截出两个字符串,再求和刷新最小值

 

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<map>
  7 #include<vector>
  8 #include<set>
  9 #include<queue>
 10 using namespace std;
 11 #define ll long long 
 12 
 13 string Sum(string a,string b)//大数加法 
 14 {
 15     //补前导零 
 16     while(a.size()<b.size())
 17         a.insert(0,"0");
 18     while(b.size()<a.size())
 19         b.insert(0,"0");
 20     
 21     string ans="";//记录结果 
 22     
 23     int jinwei=0,sum=0,yu;//运算所需 
 24     
 25     for(int i=a.size()-1;i>=0;i--)//从末尾算起 
 26     {
 27         sum=(a[i]-'0')+(b[i]-'0')+jinwei;
 28         jinwei=sum/10;
 29         yu=sum%10;
 30         ans+=(yu+'0');//加上这一位的余数 
 31     }
 32     if(jinwei)//可能多一位 
 33         ans+=(jinwei+'0');
 34     
 35     reverse(ans.begin(),ans.end());//由于是从末尾算起,需要逆置字符串 
 36     
 37     return ans;
 38 }
 39 
 40 int main()
 41 {
 42     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 43     
 44     int len,now;
 45     string str,s;
 46     string a,b;
 47     while(cin>>len)
 48     {
 49         cin>>str;
 50         
 51         string minn="0";
 52         
 53         now=len/2;
 54         for(int i=now;i>=0;i--)
 55         {
 56             if(str[i]!='0')
 57             {
 58                 a=str.substr(0,i);
 59                 b=str.substr(i,len-i);
 60                 s=Sum(a,b);
 61                     
 62                 if(minn=="0")
 63                     minn=s;
 64                 else
 65                 {//这里字符串不能直接比较大小,要先看长度,位数小的数字才小 
 66                     if(s.size()<minn.size()) 
 67                         minn=s;
 68                     else if(s.size()==minn.size())
 69                     {
 70                         if(s<minn)
 71                             minn=s;
 72                     }
 73                 }
 74                 break;
 75             }
 76         }
 77             
 78             
 79         for(int i=now+1;i<len;i++)
 80         {
 81             if(str[i]!='0')
 82             {
 83                 a=str.substr(0,i);
 84                 b=str.substr(i,len-i);
 85                 s=Sum(a,b);
 86             
 87                 if(minn=="0")
 88                     minn=s;
 89                 else
 90                 {
 91                     if(s.size()<minn.size())
 92                         minn=s;
 93                     else if(s.size()==minn.size())
 94                     {
 95                         if(s<minn)
 96                             minn=s;
 97                     }
 98                 }
 99                 break;
100             }
101         }
102         cout<<minn<<endl;
103     }
104     
105     return 0;
106 }

 

 

posted on 2019-06-17 19:02  乐逍遥xwl  阅读(322)  评论(0编辑  收藏  举报