两个超大的数字相加
function largeCount(f, t) { f += ""; t += ""; let fl = f.length, tl = t.length, arr = [], i = 0, spacing = fl - tl, len = 0, lastMan = [], fr = f.split("").reverse(), tr = t.split("").reverse(), current = 0, curr = 0, isT, maxLen; if (spacing > 0) { len = tl; lastMan = fr; maxLen = fl; } else { len = fl; lastMan = tr; maxLen = tl; } function course(current, i) { if (current > 9) { arr[i] = current % 10; arr[i + 1] = 1; return true; } else { arr[i] = current; return false; } } for (; i < len;) { curr = arr[i] ? arr[i] : 0; current = fr[i] * 1 + tr[i] * 1 + curr * 1; course(current, i++); if (i === len) { if (arr[i]) { do { curr = lastMan[i]; if (curr) { current = lastMan[i] * 1 + arr[i] * 1; isT = course(current, i++); } else { isT = false; } } while (isT && i < maxLen); } arr = [...arr, ...lastMan.slice(i)]; } } return arr.reverse().join(""); }
版权声明:本文为wuxiexy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。