我需要将这两个数字相加多少位数?


27

任务

给定两个正整数,输出以10为底加长加法将它们加在一起的进位数目。

例子

¹¹¹   <-- carries
 999
+  1
----
1000

需要三个进位。

 ¹
 348
+ 91
----
 439

需要一个进位。

测试用例

999,   1 -> 3
398,  91 -> 1
348,  51 -> 0
348,  52 -> 2
  5,  15 -> 1
999, 999 -> 3
505, 505 -> 2

计分

这是。以字节为单位的最短答案将获胜。有标准漏洞



14
建议的测试用例:(190192, 90909在进位中有中断)。
乔纳森·艾伦,

5
来自@Jenny_mathy的答案:进位数目等于(1)两个输入的数字总和之和与(2)两个输入的数字之和之和除以9之差。这是因为当有一个进位时,您从中减去10并在数字总和上加1。例如,9+9给您18,但是数字总和是9+9-10+1因为有一个进位。
JungHwan Min


我们可以假设数字适合我们的语言的int类型吗?特别是对于Python 2,我们应该处理上面reprLfor 附加数字2**63-1吗?
xnor

Answers:


21

Mathematica,46 39字节

x=Tr@*IntegerDigits;(x@#+x@#2-x@+##)/9&

输入

[348,51]

JungHwan -7个字节


堂,我真的很喜欢这种方法。(1)两个输入的数字总和的总和与(2)两个输入的数字总和的总和之间的差是进位数目的9倍,因为当有进位时,您需要从进位中减去10数字总和,然后在数字总和上加1。
JungHwan Min

我也是!感谢您的高尔夫技巧
J42161217

测试用例说[348,51]应该返回0,但是运行此代码时我得到的是56/3 ...?
numbermaniac

Welp,看来现在正在运作。不确定Mathematica之前在做什么...
numbermaniac

6

JavaScript(ES6),50个字节

修复OVS解决方案中被盗的问题

f=(a,b,c=0)=>a|b|c&&c+f(a/10,b/10,a%10+b%10+c>=10)

说明

f=(a,b,c=0)=>                                      Function taking two numbers and optional carry
             a|b|c                                 If a number or the carry are nonzero
                  &&                               Then
                    c+f(a/10,b/10,a%10+b%10+c>=10) Return carry plus all other carries

进行说明

a%10+b%10+c     Sum of mod 10 of the numbers and c - remember these are not floordiv'ed
           >=10 Greater than or equals to 10 (we can't use greater than 9 here)

f=(a,b,c=0)=>a|b|c&&c+f(a/10,b/10,a%10+b%10+c>=10)
console.log([[999,1],[398,91],[348,51],[348,52],[5,15],[999,999],[256,64],[190192,90909]].map(test=>`${(test[0]+'').padStart(6,' ')}, ${(test[1]+'').padStart(6,' ')} -> ${f(...test)}`).join('\n'));


1
348 , 52应该是2
Toto

这是如何运作的?你能补充一个解释吗?
Arjun

5

C(gcc),65个字节

i,l;f(a,b){for(i=l=0;a+b;a/=10,b/=10)i+=a%10+b%10+l>9?l=1:0;a=i;}

在线尝试!


您无需初始化全局变量。
user1502040

如果在函数内部使用它们而未初始化,则需要@ user1502040。
Leaky Nun

1
我只是在这里进行初始化:变量自动进行零初始化,但只能进行一次初始化,因此,如果函数多次运行,PPCG上的函数提交必须起作用,因此需要手动将它们清零,以利于第二次初始化及后续运行。

5

果冻 13 12 11  9 字节

移植Jenny_mathy的数学答案为 -1字节。
-2通过更好的打高尔夫球增加字节数:p

;SN$DFS:9

请参阅测试套件

怎么样?

;SN$DFS:9 - Main link: list of numbers, [a,b]     e.g.   [348,53]
   $      - last two links as a monad
 S        -   sum                                            401
  N       -   negate                                        -401
;         - concatenate                             [348,53,-401] 
    D     - convert to decimal lists     [[3,4,8],[5,3],[-4,0,-1]]
     F    - flatten                           [3,4,8,5,3,-4,0,-1]
      S   - sum                                               18
       :9 - integer divide by nine                             2

我的12字节解决方案...

:⁵+
DUSç\>9S

单链链接,取一对整数并以整数形式返回进位数目。

虽然可能有更短的方法!有!

在线尝试!或查看测试套件

怎么样

:⁵+ · Link 1: perform a carry: right-column's-digit-sum, a; left-colum's-digit-sum; b
 ⁵  · literal 10
:   · a integer-divided by 10 - the carry amount
  + · add to b

DUSç\>9S · Main link: list of summands        e.g. [348,52]
D        · convert to decimal lists                [[3,4,8],[5,2]]
 U       · upend (reverse each)                    [[8,4,3],[2,5]]
  S      · sum (add the digits up)                 [10,9,3]
    \    · cumulative reduce with:
   ç     ·   last link (1) as a dyad               [10,10,4]
      9  · literal 9
     >   · greater than?                           [ 1, 1,0]
       S · sum                                     2

DS... 的许多用途
Egg the Outgolfer

4

Python,48个字节

f=lambda a,b,m=1:m<1e99and(~a%m<b%m)+f(a,b,m*10)

在线尝试!

对于每个位置值m=1, 10, 100, ..., 10**99,检查该位置值是否有进位。溢出检查a%m+b%m>=m缩短为~a%m<b%m

一个更好的45字节变体,它浮动a并向下b移位

f=lambda a,b:a+b and(a%1+b%1>=1)+f(a/10,b/10)

可悲的是遇到了浮点精度问题。


您不能将其a+b<m用作终止条件吗?
尼尔

@Neil需要<=更长。
xnor

1e99and讨厌
乔纳斯·谢弗

4

JavaScript(ES6),53 45字节

f=(a,b,d=1)=>a+b<d?0:(a%d+b%d>=d)+f(a,b,d*10)

通过添加一个额外的虚无迭代来节省进1字节,以进行进位。通过使用@xnor的进位检查节省了7个字节。我也有一个更优雅的45字节版本,但是它有浮点数错误;将其翻译成具有精确十进制算术的语言,效果会很好:

f=(a,b,c=a+b)=>c<1?0:a%1+b%1-c%1+f(a/10,b/10)

3

Python 2,55个字节

f=lambda a,b,c=0:c+a+b and c+f(a/10,b/10,a%10+b%10+c>9)

在线尝试!


1
正如@JonathanAllan指出的那样,如果您使用了对函数的调用,则也必须对其进行声明。话虽这么说,你的答案是55个字节
Xcoder先生

@ Mr.Xcoder修复了问题
ovs


2

Neim,10个字节

𝔸𝐣𝐬₁₂𝔻𝐬𝕊λ𝕍

说明:

𝔸            𝔸ppend the two inputs into a list
 𝐣            𝐣oin them together
  𝐬           𝐬um the characters
   ₁₂         Push the first input, then the second
     𝔻        A𝔻d.
      𝐬       𝐬um the characters
       𝕊      𝕊ubtract
         𝕍    Integer di𝕍ision by
        λ     nine (variable)

试试吧!

替代解决方案,也是10个字节:

𝔸D𝐣𝐬S𝐬𝐬𝕊9𝕍

说明:

𝔸             𝔸ppend the two inputs into a list
 D            Duplicate
  𝐣            𝐣oin
   𝐬           𝐬um
    S          Swap
     𝐬         𝐬um
      𝐬        𝐬um the characters
       𝕊       𝕊ubtract
         𝕍     integer di𝕍ide by
        λ       nine (variable)

试试吧!



0

Braingolf,20个字节

VR{.M}d<d&+v+d&+c-9/

在线尝试!

使用与其他所有人相同的方法。

如果我有远见,可以d使用贪婪修饰符保存一个字节或2个字节,那么下一次我可以替换d<d&dah。

说明

VR{.M}d<d&+v+d&+c-9/  Implicit input from commandline args
VR                    Create stack2 and return to stack1
  {..}                Foreach loop, runs on each item in stack1
   .M                 Duplicate and move duplicate to stack2
      d<d             Split both items on stack into digits
         &+           Sum entire stack
           v+         Switch to stack2 and sum last 2 items on stack
             d&+      Split result into digits and sum all digits
                c     Collapse stack2 into stack1
                 -    Subtract last item from 2nd to last
                  9/  Divide by 9
                      Implicit output of last item on stack
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.