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

高精度整数加法

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

const rl = require("readline").createInterface({ input: process.stdin });

let lines = [];
rl.on("line", (line) => {
    lines.push(line);
}).on("close", () => {
    function addString(str1, str2) {
        let result = [];
        let carry = 0;
        let i = str1.length - 1;
        let j = str2.length - 1;

        while (i >= 0 || j >= 0 || carry > 0) {
            const digital1 = i >= 0 ? parseInt(str1[i]) : 0;
            const digital2 = j >= 0 ? parseInt(str2[j]) : 0;
            const sum = digital1 + digital2 + carry;
            carry = Math.floor(sum / 10);
            result.unshift(sum % 10);

            i--;
            j--;
        }
        return result.join("");
    }
    const [str1, str2] = lines;
    const res = addString(str1, str2);
    console.log(res);
});

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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