题解 | #大数加法#

大数加法

http://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475

#include<sstream>
#include<string>
using namespace std;
class Solution{
    public:
     string solve(string s,string t){
        int lens=s.size();
        int lent=t.size();
        if(lens==0){
            return t;
        }
        if(lent==0){
            return s;
        }
        while(lent<lens){
            t="0"+t;
            lent++;
        }
        while(lens<lent){
            s="0"+s;
            lens++;
        }
        string res;
        int carry=0;
        int len=lens;
        for(int i=len-1;i>=0;i--){
            int temp=(s[i]+t[i]-'0'-'0'+carry);
            res=char(temp%10+'0')+res;
            carry=temp/10;
        }
        if(carry==1){
            res=char(carry+'0')+res;
        }
        return res;
    }
};
全部评论

相关推荐

11-27 21:29
已编辑
武汉理工大学 Java
点赞 评论 收藏
分享
11-28 16:00
已编辑
武汉理工大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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