B. Split a Number(字符串加法)
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.
The first line contains a single integer ll (2≤l≤1000002≤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.
Print a single integer — the smallest number Dima can obtain.
7
1234567
1801
3
101
11
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.
题解:从中间往两边分出两个分支,取最优解即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string cal(string a,string b)//字符串加法,模拟数的加法即可
{
string ans="";
int pos1=a.size()-,pos2=b.size()-;
int last=,x=;
while(){
if(pos1<&&pos2<)break;
if(pos1<&&pos2>=){
while(pos2>=){
x=b[pos2--]-''+last;
if(x>=){
last=x/;
x%=;
}
else
last=;
ans+=x+'';
}
break;
}
if(pos2<&&pos1>=){
while(pos1>=){
x=a[pos1--]-''+last;
if(x>=){
last=x/;
x%=;
}
else
last=;
ans+=x+'';
}
break;
}
x=a[pos1--]-''+b[pos2--]-''+last;
if(x>=){
last=x/;
x%=;
}
else
last=;
ans+=x+'';
}
if(last)
ans+=last+'';
return ans;
}
int main()
{
int n;
cin>>n;
string s;
cin>>s;
int pos1=n/,pos2=n/+;
while(s[pos1]==''&&pos1>)pos1--;
while(s[pos2]==''&&pos2<n-)pos2++; string a=s.substr(,pos1);
string b=s.substr(pos1,s.size());
string ans=cal(a,b);
reverse(ans.begin(),ans.end());
string aa=s.substr(,pos2);
string bb=s.substr(pos2,s.size());
string anss=cal(aa,bb);
reverse(anss.begin(),anss.end());
if(s[pos2]=='')//特判后一部分不能分的情况,如果想到的话,这个题比赛的时候就能做出来了丫丫丫
return cout<<ans<<endl,;
if(ans.size()<anss.size())cout<<ans<<endl;
else if(ans.size()>anss.size())cout<<anss<<endl;
else{
if(ans<anss)
cout<<ans<<endl;
else
cout<<anss<<endl;
}
return ;
}
B. Split a Number(字符串加法)的更多相关文章
- Codeforces Round #567 (Div. 2)B. Split a Number (字符串,贪心)
B. Split a Number time limit per test2 seconds memory limit per test512 megabytes inputstandard inpu ...
- Codeforces C. Split a Number(贪心大数运算)
题目描述: time limit per test 2 seconds memory limit per test 512 megabytes input standard input output ...
- Codeforces Round #567 (Div. 2) B. Split a Number
Split a Number time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- delphi string.split 按照任意字符串分割语句
delphi string.split 按照任意字符串分割语句 1.就是把一个指定的字符串用指定的分割符号分割成多个子串,放入一个 TStringList 中 function ExtractStri ...
- Split the Number(思维)
You are given an integer x. Your task is to split the number x into exactly n strictly positive inte ...
- JS对象 字符串分割 split() 方法将字符串分割为字符串数组,并返回此数组。 语法: stringObject.split(separator,limit)
字符串分割split() 知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如 ...
- Java split 根据指定字符串分隔成list数组的用法
String str="Java string split test"; String[] strarray=str.split(" ");//得到一 ...
- Swift3.0 split函数切割字符串
我们先看函数的原型: public func split(separator: Self.Iterator.Element, maxSplits: Int = default, omittingEmp ...
- ACdream 1188 Read Phone Number (字符串大模拟)
Read Phone Number Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Sub ...
随机推荐
- (递归)P1025 数的划分
题解: #include<iostream>using namespace std;int ret=0,m_n;void p(int n,double k,int j){ if(k==1) ...
- 51nod1007:正整数分组 DP
1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 ...
- HTTP协议(四):首部
前言 作者说:上一节中介绍了HTTP报文中的状态码,这一节同样是对报文的补充,介绍的是HTTP首部字段.不过,你如果是第一次见到这个东西,肯定会特别疑惑,什么是HTTP首部? <图解HTTP&g ...
- CTF -攻防世界-web高手区-ics-06
打开网址 根据题意点开报表中心(因为其他的点开都一样,不信你试试) 会看见id =1 想到burp爆破id 所以打开burp抓包(不会抓包的百度 或者看我web新手区,有一题就有抓包 我说的很详细) ...
- {转} MJPG流媒体在HTML5的呈现方案
最近碰到的需求:监控探头视频呈现到html页面上. 视频源协议:HLS; 视频源格式:Motion JPEG 简称 MJPG; 其中Motion JPEG(M-JPEG或MJPEG,Motion Jo ...
- selenium登陆qq邮箱页面
from selenium import webdriver driver = webdriver.Chrome() driver.get('https://mail.qq.com/cgi-bin/l ...
- Python pip安装时You are using pip version 9.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
在我们安装第三方库的时候会在结尾出现如下两行内容 You are using pip version 9.0.1, however version 18.0 is available. You sho ...
- MySQL--通过.frm和.ibd对mysql数据恢复
转载:http://bbs.csdn.net/topics/392114182 例如说 现在要恢复user表1.先建立和之前user表一样的表结构.就是执行create table user .... ...
- 91.一次性处理多条数据的方法:bulk_create,update,delete
(1)bulk_create: 可以一次性的创建多个对象 示例代码如下: from django.http import HttpResponse from .models import Pulish ...
- Java目录下文件夹的含义和运行机制
Java安装目录下的各个文件夹的意义 >bin 放置Java所有的可执行文件 >include 一些C语言的头文件 >jre Java的运行环境 >lib Java的类库文件 ...