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.

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.

 

 题解:从中间往两边分出两个分支,取最优解即可。

#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;
                ans+=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;
                ans+=x+'0';
            }
            break;
        }
        x=a[pos1--]-'0'+b[pos2--]-'0'+last;
        if(x>=10){
            last=x/10;
            x%=10;
        }
        else
            last=0;
        ans+=x+'0';
    }
    if(last)
        ans+=last+'0';
    return ans;
}
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')//特判后一部分不能分的情况,如果想到的话,这个题比赛的时候就能做出来了丫丫丫
        return cout<<ans<<endl,0;
    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 0;
}

 

转载于:https://www.cnblogs.com/cherish-lin/p/11035894.html

内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值