【Edabit 算法 ★★★★★】【两个大整数相加】Recursion: Sum of Two Numbers (With A Twist!)

Recursion: Sum of Two Numbers (With A Twist!)

Instructions

This is an "expert" challenge!!! Why is a sum of two numbers an "expert" challenge!!! Well, the numbers can have 1000 digits or even beyond such count...

So, what's the twist? You have to do the summation as if you're doing it manually on a piece of paper, thus, the conversion of the numeric string to numeric literal is basically disallowed.

Examples
javascript 复制代码
sum("12132000", "12171979") // "24303979"

sum("4666", "544") // "5210"

sum("1521512512512512515", "898989898989988998899898") // "898991420502501511412413"

sum("5125515215521515", "125261616261626") // "5250776831783141"

sum("6666666666666666666666666666", "99999999999999999999999") // "6666766666666666666666666665"

sum("123456789123456789123456789", "987654321987654321987654329876543") // "987654445444443445444443453333332"
Notes
  • Remember how to sum two numbers ON A PAPER, such is the process.
  • Your function must run in less than 10 seconds because it has a time limit.
  • The use of Number class such as BigInt is disallowed -- it will defeat the purpose and the level of difficulty associated to this challenge.
  • You are expected to solve this challenge recursively.
Solutions
javascript 复制代码
const sum = (a, b,c=0) => {
    if(!a && !b && c == 0){
        return ''
    }
    let aa = (a)?(a.slice(-1)-0):0;
    let bb = (b)?(b.slice(-1)-0):0;
    let s = aa + bb + c
    c = s>9?1:0;
    return sum(a.slice(0,-1),b.slice(0,-1),c)+(s%10)
}
TestCases
javascript 复制代码
let Test = (function(){
    return {
        assertEquals:function(actual,expected){
            if(actual !== expected){
                let errorMsg = `actual is ${actual},${expected} is expected`;
                throw new Error(errorMsg);
            }
        },
        assertNotEquals:function(actual,expected){
            if(actual === expected){
                let errorMsg = `actual is ${actual},${expected} is expected,they are equals`;
                throw new Error(errorMsg);
            }
        }

    }
})();

const isRecursive = src => (`${src}`.match(RegExp(`${src.name}`, 'gm'))||[]).length > +(/function/.test(src))
Test.assertNotEquals(isRecursive(sum), false, "Recursion is required!")

let [actualParam, expectedParam] = [
    [
        ["12132000", "12171979"], ["4666", "544"], ["1521512512512512515", "898989898989988998899898"],
        ["5125515215521515", "125261616261626"], ["6666666666666666666666666666", "99999999999999999999999"],
        ["123456789123456789123456789", "987654321987654321987654329876543"], ["51", "512"],
        ["46580672134861691487886856201063433530317493541984174240640117078384844027455455145995264175402994424834479825796316174329467969102257360195385044875023188313698661902232816682563450684527972706431205", "20129647448213526330992199933412026717951269059875880213489467074335368047371342207724579931208231032969760043956811494704380198848377355718984761723730087673439394159054420344427904875384087249296946"],
        ["9128242816391792390367394318238609154929962550133827657886034828979294413033450307173793450924762143201991300288127408763421237279633517929936847079257713141254694944681428142978110027357322312404627593110196423560326537881370897768020382035189644680256824659171348515208671339529370866296929702167647163038519576331084019822103309755374561623148508523431380245253765653509318684179663600476971689801", "5920641803160990513445202815794518152101247319199211634010324208708552138569594568355624738331704740605556159925350097568289164018471525773848461636579024644391854277092707811953956319566890527925989019562020260846251250663758330856266051985217733863782039893158278545291027890391152027767054280498870038607952519452004179810592466387736659835203110761590431605238080432136468832137768846891022675668"],
        ["24050292702239538714424762926989391091054882494797379961190995916419743076846190252322346159955367441832937735205583340798028856059731163836333572978411075895848661770468080051146869104337532213474993926921843996913706778627924709600100860461421587774140722793995230660233453392717776973617724296276959982403528208646617679528431859423982314682036555772485961534695291544266936268924447065901465864784710200365748971482974309578528938725051499783831735126491173069337708438252812165533370751831832345511906521502270947947504198101881866181323122256768406228518806385974069302887460096561970477705646199939356606923830372891944037065847071308700107176794261922547322922787636783283829766004529060524539701495229943211611134317723328686844713489223961776582365551050940511119736023799724295560712462775", "32509558457800134082157248923945369106923458582597107662799282973299414325101867489152494482453431986091527569819907127801971978514325573048496062254539304686542784848485721075326183780905103371293505027494462848297668132430517455611475977074377557603798687767852907762254040565866554962529868705211874976201522692137999060766380670853123585546115851899681725949430620582898894763596679656710337017496618525630124192131724949516793735384953877012134805554272494110981312910720545085389502607669244051838541600298937410166860469735980721786025686375273866096353307350361733480286161315578348192126560652007559649899391482681958298876030894402469363045420883354390088168624736238621292726748213941087878476447239860986699971354976451472810499473465596980950104110861804910439253305564075282187561761831"], ["6809632763916891310120420620586174664812635530867937840217826224568347127990297761160448320170180761000408251375466103628257285208000720825374647976159883503477330018358281993325677527017557058976068333642868984291739483975621870841667306204393840341405348294943813620516550885907643492592684427984599974017532776047374095558566398217709965020793366640224373810304569301398035374905004719899959454164388545839944663454514841628001498947789419801699846006819006975473515954356883318037820103882153723720601881130291354697184471999967716687686218472392686266650318568026273517115609143992129773683289728842208471476663161973115343223846428988702456038387814984825150310010062042877806640480710525528241328472095324930106698917227353621406980530110747055257101350540514070478171412772633866196018240727864894180766177622426511795549818029880640303307250349733954619463752541929370662897223878043296358089043269919860180819045946942402216596728187295625046088616265162395417587774677163023091414012232562","8580486073000341176464481569509088314164648516198308588306341581886828084652373143824178061446100845207932260299989735098857835077503613944792671258723699144053082578718738452102088425930223680001667115358790806513480705990372645625620985201064789499679080382290229510353519900434457951354146351017356497038201676685010533295544810336326460878234636045306593283716955770214654833569407250862467764241301491075773878798755773232056371316349556337923582112418745691372603517681014116333538448075872043025194944464917374343870889032601696418177538646357828263727642286052251539150438138687143116934852022178306775340622808992992303183802309493297583818627523253611781850609208464293666564468773980578995182119812585094120227322948794646916387478317146222403428768533848504528795446827274517795896686680232498391880209937677390643332411043134407239601699748922010617022901854909440001914242164998598960486713118948710525156592829968225269277615537170861633579862039532587858207573589726753466950083211842"]
    ], [
        "24303979",
        "5210",
        "898991420502501511412413",
        "5250776831783141",
        "6666766666666666666666666665",
        "987654445444443445444443453333332",
        "563",
        "66710319583075217818879056134475460248268762601860054454129584152720212074826797353719844106611225457804239869753127669033848167950634715914369806598753275987138056061287237026991355559912059955728151",
        "15048884619552782903812597134033127307031209869333039291896359037687846551603044875529418189256466883807547460213477506331710401298105043703785308715836737785646549221774135954932066346924212840330616612672216684406577788545129228624286434020407378544038864552329627060499699229920522894063983982666517201646472095783088199632695776143111221458351619285021811850491846085645787516317432447367994365469",
        "56559851160039672796582011850934760197978341077394487623990278889719157401948057741474840642408799427924465305025490468600000834574056736884829635232950380582391446618953801126473052885242635584768498954416306845211374911058442165211576837535799145377939410561848138422487493958584331936147593001488834958605050900784616740294812530277105900228152407672167687484125912127165831032521126722611802882281328725995873163614699259095322674110005376795966540680763667180319021348973357250922873359501076397350448121801208358114364667837862587967348808632042272324872113736335802783173621412140318669832206851946916256823221855573902335941877965711169470222215145276937411091412373021905122492752743001612418177942469804198311105672699780159655212962689558757532469661912745421558989329363799577748274224606",
        "15390118836917232486584902190095262978977284047066246428524167806455175212642670904984626381616281606208340511675455838727115120285504334770167319234883582647530412597077020445427765952947780738977735449001659790805220189965994516467288291405458629841084428677234043130870070786342101443946830779001956471055734452732384628854111208554036425899028002685530967094021525071612690208474411970762427218405690036915718542253270614860057870264138976139623428119237752666846119472037897434371358551958025766745796825595208729041055361032569413105863757118750514530377960854078525056266047282679272890618141751020515246817285970966107646407648738482000039857015338238436932160619270507171473204949484506107236510591907910024226926240176148268323368008427893277660530119074362575006966859599908383991914927408097392572646387560103902438882229073015047542908950098655965236486654396838810664811466043041895318575756388868570705975638776910627485874343724466486679668478304694983275795348266889776558364095444404"
    ]
]
for (let i in actualParam) Test.assertEquals(sum(...actualParam[i]), expectedParam[i])
相关推荐
心运软件7 小时前
银行客户流失预测(Python 完整实战)
算法
邪神与厨二病7 小时前
牛客周赛 Round 153
python·算法
qizayaoshuap9 小时前
# ❌ 井字棋 — 鸿蒙ArkTS Minimax AI算法与博弈系统设计
人工智能·算法·华为·harmonyos
皓月斯语10 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
atunet10 小时前
树状结构在查询优化中的作用与实现细节7
算法
徐凤年_11 小时前
rog_map参数理解
算法
春日见11 小时前
算法与数据结构----哈希表
数据结构·人工智能·算法·机器学习·自动驾驶·哈希算法·散列表
叩码以求索11 小时前
统计按位或能得到最大值的子集数目(一)
数据结构·算法
tachibana212 小时前
hot100 数组中的第K个最大元素(215)
java·数据结构·算法·leetcode
txzrxz12 小时前
单调队列讲解
数据结构·c++·算法·单调队列