使用ASCII艺术图可视化长时间添加


13

完全不受视觉化与ASCII艺术的长期划分的启发;)

您的工作是要用ASCII艺术表现出长手的加法。您可以通过将列从右到左相加,将那些位置的值放在结果中,然后将十位数移到下一列的顶部来解决长期累加问题。

输入值

输入只要输入2到9个数字,基本上就可以采用您想要的任何格式。

输出量

此处的格式可能与您在学校学习的方式相符:

carry row
 number1 
 number2
     ...
+   numX
--------
  result

您可以在此处拥有几乎任何数量的尾随空白;)

例子

50, 50

1
 50
+50
---
100


1651, 9879

1111
 1651
+9879
-----
11530

6489789, 9874, 287

   1122
 6489789
    9874
+    287
--------
 6499950

用ASCII艺术可视化长时间的减法:在6个月内直接出现
CalculatorFeline

不完全是,它在我的名单上;)
J Atkin

1
实际上,我被教导要把进位行置于结果之下。
尼尔

1
我们应该如何处理9+9+9+9+9+9+9+9+9+9+9+9+9
Downgoat

1
@Downgoat ... you take from 2 to 9 numbers as input...
PurkkaKoodari

Answers:


5

Pyth,59 58字节

L.[dJhl`eSQ`b:jk_.u/+NsYT.t_MjRTQ00\0djbyMPQXyeQ0\+*J\-ysQ

在线尝试。 测试套件。

太久了 应该多打高尔夫球。

说明

L                  helper function y = lambda b:
        eSQ          largest number in input
      l`             length as string
     h               increment
    J                save to J
 .[d       `b        pad argument with spaces to that length

                             carry row:
                jRTQ           each input to base 10
              _M               reverse each result
            .t      0          transpose, padding with zeroes
    .u               0         cumulative reduce from 0:
         sY                      sum digits of column
       +N                        add previous carry
      /    T                     floor-divide by 10
   _                           reverse
 jk                            join by ""
:                     \0d      replace 0 by space

          number rows:
    PQ      all input numbers but last one
  yM        pad to correct length
jb          print on separate lines

           last number row:
  eQ         last input number
 y           pad to correct length
X   0\+      change first char to +

        separator row:
 J        width of input (saved in helper)
* \-      that many dashes

       result row:
 sQ      sum of inputs
y        pad to correct length

1

批处理,326字节

字节数当然不包括说明。

@echo off
set t=%*                            Get the space separated parameters
set t=%t: =+%                       Change the spaces into + signs
set/at=%t%,l=p=1                    Add together, and initialise length and power
set c=                              Carry string
set d=-                             Dash string
:l                                  Loop though each power of 10
set/al+=1,p*=10,s=t/p               Divide the total by the power
for %%n in (%*)do set/as-=%%n/p     Subtract each parameter divided
set c=%s%%c%                        Anything left must have been carried
set d=-%d%                          Add a - to the line of dashes
if %p% leq %t% goto l               Keep going until we run out of powers
echo(%c:0= %                        Delete any zeros in the carry and output it
:i                                  Loop through each parameter
set n=%d:-= %%1                     Pad it with a whole bunch of spaces
call set n=%%n:~-%l%%%              Extract the rightmost characters
if "%2"=="" set n=+%n:~1%           Insert a + before the last parameter
echo %n%                            And output it
shift                               Move to the next parameter
if not "%1"=="" goto i              Until they are all consumed
echo %d%                            Output the line of dashes
echo  %t%                           Output the total (with an indent for the +)

0

JavaScript(ES6),199个字节

a=>[[...t=` `+a.reduce((t,n)=>t+n)].map((_,i)=>a.reduce((c,n)=>c-n[i],90+t[i])%10||` `),a=a.map(n=>(` `.repeat(l=t.length)+n).slice(-l))).join``,...a,`-`.repeat(l),t].join`\n`.replace(/ (?=.*\n-)/,`+`)

其中第一个\n表示原义换行符,而第二个则表示两个字符的正则表达式转义序列。说明:

a=>[                            Accept an array of numbers
 [...                           Split the total into digits
  t=` `+a.reduce((t,n)=>t+n)    Calculate the total and add a space
 ].map((_,i)=>a.reduce((c,n)=>  For each column
  c-n[i],90+t[i])               Subtract the column from the total
  %10||` `),                    Deduce the carry that was needed
  a=a.map(n=>                   For each input value
   (` `.repeat(l=t.length)+n)   Pad to the length of the total
    .slice(-l))                 Remove excess padding
 ).join``,                      Join the carries together
 ...a,                          Append the padded input values
 `-`.repeat(l),                 Append the dividing line
 t].join`\n`                    Append the total and join together
  .replace(/ (?=.*\n-)/,`+`)    Insert the + on the line above the -

的进位计算的工作原理是占全部数字,前缀90,减去所有的输入值的数字在该列中,并且取结果模10(该前缀是90而不是9使前导列产生进位的空白空间)。

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.