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

Codeforces Round #567 (Div. 2)ios

 

B. Split a Numbergit

 

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.ide

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.spa

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.code

Inputblog

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

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

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.字符串

Outputget

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 }
相關文章
相關標籤/搜索