题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
function bigIntAdd(num1, num2) { // 补零 const diff = num1.length - num2.length; if (diff > 0) { for (let i = 0; i < diff; i++) { num2 = `0${num2}`; } } else if (diff < 0) { for (let i = 0; i < Math.abs(diff); i++) { num1 = `0${num1}`; } } let sum = ''; let jinwei = 0; const num1ReverseArr = num1.split('').reverse(); const num2ReverseArr = num2.split('').reverse(); num1ReverseArr.forEach((num, index) => { let temp = parseInt(num, 10) + parseInt(num2ReverseArr[index], 10) + jinwei; if (temp >= 10) { jinwei = 1; temp = temp - 10; } else { jinwei = 0; } sum = `${temp}${sum}`; }); return jinwei > 0 ? `${jinwei}${sum}` : sum; } while (line = readline()) { const line2 = readline(); print(bigIntAdd(line, line2)); }