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

2023-10-22 09:04

本文主要是介绍【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
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
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 + cc = s>9?1:0;return sum(a.slice(0,-1),b.slice(0,-1),c)+(s%10)
}
TestCases
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])

这篇关于【Edabit 算法 ★★★★★★】【两个大整数相加】Recursion: Sum of Two Numbers (With A Twist!)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/260445

相关文章

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

秋招最新大模型算法面试,熬夜都要肝完它

💥大家在面试大模型LLM这个板块的时候,不知道面试完会不会复盘、总结,做笔记的习惯,这份大模型算法岗面试八股笔记也帮助不少人拿到过offer ✨对于面试大模型算法工程师会有一定的帮助,都附有完整答案,熬夜也要看完,祝大家一臂之力 这份《大模型算法工程师面试题》已经上传CSDN,还有完整版的大模型 AI 学习资料,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

PTA求一批整数中出现最多的个位数字

作者 徐镜春 单位 浙江大学 给定一批整数,分析每个整数的每一位数字,求出现次数最多的个位数字。例如给定3个整数1234、2345、3456,其中出现最多次数的数字是3和4,均出现了3次。 输入格式: 输入在第1行中给出正整数N(≤1000),在第二行中给出N个不超过整型范围的非负整数,数字间以空格分隔。 输出格式: 在一行中按格式“M: n1 n2 ...”输出,其中M是最大次数,n

dp算法练习题【8】

不同二叉搜索树 96. 不同的二叉搜索树 给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。 示例 1: 输入:n = 3输出:5 示例 2: 输入:n = 1输出:1 class Solution {public int numTrees(int n) {int[] dp = new int

最大流=最小割=最小点权覆盖集=sum-最大点权独立集

二分图最小点覆盖和最大独立集都可以转化为最大匹配求解。 在这个基础上,把每个点赋予一个非负的权值,这两个问题就转化为:二分图最小点权覆盖和二分图最大点权独立集。   二分图最小点权覆盖     从x或者y集合中选取一些点,使这些点覆盖所有的边,并且选出来的点的权值尽可能小。 建模:     原二分图中的边(u,v)替换为容量为INF的有向边(u,v),设立源点s和汇点t