题解 | #高精度整数加法#

高精度整数加法

https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6

#include <algorithm>
#include <iostream>
using namespace std;


int main() 
{
    string str1,str2;
    while(cin>>str1>>str2)
    {
        int i=str1.size()-1,j=str2.size()-1;
        int carry=0;
        string ans;
        while(0<=i&&0<=j)
        {
            int temp=(str1[i]-'0')+(str2[j]-'0')+carry;
            --i;--j;
            ans.push_back(
                temp%10+'0'
            );
            carry=temp/10;
        }
        while(0<=i)
        {
            int temp=(str1[i]-'0')+carry;
            --i;
            ans.push_back(
                temp%10+'0'
            );
            carry=temp/10;
        }
        while(0<=j)
        {
            int temp=(str2[j]-'0')+carry;
            --j;
            ans.push_back(
                temp%10+'0'
            );
            carry=temp/10;
        }
        if(carry!=0)
        {
            ans.push_back(
                carry+'0'
            );
        }
        reverse(ans.begin(), ans.end());
        cout<<ans<<endl;
    }
    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
投了多少份简历才上岸
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
仁者伍敌:服务员还要脱颖而出,这是五星级酒店吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务