两个超大的数字相加
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(""); }
声明:严禁抄袭,欢迎转载!不过请带上博文链接!
或者你有更好的实现方式、或者你觉得有可以优化的地方,不妨评论讨论下!觉得得到帮助的可以点个推荐,让更多人也可以得到帮助。