输出部分产品


17

长乘法中,将数字相乘后,剩下的是部分乘积,在此挑战中,您将输出这些部分乘积。

因为长乘法很长,所以要补偿您的代码将需要尽可能短。

例子

34, 53
102, 1700

48, 38 
384, 1440

361, 674
1444, 25270, 216600

0, 0
0

1, 8
8

技术指标

  • 输入/输出可以采用任何合理的格式,例如数组,逗号分隔的字符串(或任何其他不是数字的定界符),列表,函数参数等。
  • 部分产品必须按升序排列。
  • 如果部分产品是0,则可以选择是否要输出。

这是因此以字节为单位的最短代码胜出!


我假设数字可以是字符串,对吗?
Mama Fun Roll

0,0测试用例使其变得更加困难。
xnor 2015年

预期的结果是12, 102什么?大多数答案似乎都回来了24, 0, 1200
丹尼斯2015年

@丹尼斯24, 0, 1200很好。我将在发布中指定
Downgoat 2015年

Answers:


4

果冻,10 个字节

DU×µLR’⁵*×

在线尝试!

怎么运行的

DU×µLR’⁵*×  Left argument: multiplier -- Right argument: multiplicant

D           Convert the multiplier to base 10 (array of digits).
 U          Reverse the array.
  ×         Multiply each digit by the multiplicant.
   µ        Begin a new, monadic chain. Argument: A(array of products)
    L       Get the length of A.
     R      Turn length l into [1, ..., l].
      ’     Decrement to yield [0, ..., l-1].
       ⁵*   Compute 10**i for each i in that range.
         ×  Hook; multiply the powers of ten by the corresponding elements of A.

3
我猜这种语言的名字来自于这样的事实,它使每个人都感到果冻。
geokavel 2015年

7

Pyth,12个字节

.e**Qsb^Tk_w

测试套件

将输入换行符分隔开,例如

361
674

说明:

.e**Qsb^Tk_w
                Implicit: Q = eval(input()),T = 10
           w    Input the second number as a string.
          _     Reverse it.
.e              Enumerated map, where b is the character and k is the index.
     sb         Convert the character to an int.
   *Q           Multiply by Q.
  *    ^Tk      Multiply by T ^ k. (10 ^ index)

4

JavaScript(ES7),48个字节

(a,b)=>[...b+""].reverse().map((d,i)=>10**i*a*d)

ES6(56字节)

(a,b)=>[...b+""].reverse().map((d,i)=>a*d+"0".repeat(i))

说明

以数字形式返回部分乘积的数组。

(a,b)=>
  [...b+""]    // convert the multiplier to an array of digits
  .reverse()   // reverse the digits of the multiplier so the output is in the right order
  .map((d,i)=> // for each digit d of the multiplier
    10**i      // get the power of ten of the digit
      *a*d     // raise the product of the digit to it
  )

测试

测试使用Math.pow而不是**使其在标准浏览器中运行。


3

Lua,72 68字节

b=arg[2]:reverse()for i=1,#b do print(arg[1]*b:sub(i,i)*10^(i-1))end

3

APL,21字节

{⍺×x×10*1-⍨⍳≢x←⊖⍎¨⍕⍵}

这是一个二进位函数,该函数在左侧和右侧接受整数并返回一个数组。要调用它,请将其分配给变量。

说明:

             x←⊖⍎¨⍕⍵} ⍝ Define x to be the reversed digits of the right input
     10*1-⍨⍳≢         ⍝ Generate all 10^(1-i) for i from 1 to the number of digits
{⍺×x×                 ⍝ Multiply the right input by the digits and the powers of 10

1
⍎¨⍕非常聪明。
丹尼斯

2

05AB1E,15个字节

码:

VDgUSXFTNmY**=\

说明:

VDgUSXFTNmY**=\

V                 # Assign the input to Y
 D                # Duplicate of input, because the stack is empty
  g               # Pushes the length of the last item
   U              # Assign the length to X
    S             # Split the last item
     X            # Pushes X (length of the last item)
      F           # Creates a for loop: for N in range(0, X)
       TNm        # Pushes 10 ^ N
          Y       # Pushes Y (first input)
           *      # Multiplies the last two items
            *     # Multiplies the last two items
             =    # Output the last item
              \   # Discard the last item

2

Pyth,26个字节

DcGHKjHTFNJlK*G*@Kt-JN^TN

这定义了一个函数c,使其接受2参数,并且该函数打印部分乘积。

DcGHKjHTFNJlK*G*@Kt-JN^TN
DCGH                      Define function c(G, H)
    KjHT                  Set K to the list of digits converting H to base 10
        FNJlK             Set J to the length of K and loop with variable N
                          (Implicit: print)
             *G*@Kt-JN    Calculates the partial product
                      ^TN Raising it to the appropriate power of 10

1

MATL,18字节

ij48-tn:1-P10w^**P

编译器(5.1.0)的作品在Matlab和倍频。

每个数字在单独的行上输入。

>> matl ij48-tn:1-P10w^**P
> 361
> 674
1444  25270 216600

说明

i           % input first number (say 361)
j           % input second number, interpreted as a string (say '674')
48-         % subtract '0' to obtain vector of figures (gives [6 7 4])
tn:1-P      % vector [n-1, ... 1, 0] where n is the number of figures (gives [2 1 0])
10w^        % 10 raised to that, element-wise (gives [100 10 1])
*           % multiply, element-wise (gives [600 70 4])
*           % multiply (gives 361*[600 70 4], or [216600 25270 1444])
P           % flip vector ([1444 25270 216600]). Implicitly display

1

哈斯克尔,60 57 54字节

g x=zipWith(\b->(x*10^b*).read.pure)[0..].reverse.show

.show如果我可以将第二个数字作为字符串,则少5个字节(除去)。

用法示例: g 361 674 -> [1444,25270,216600]

yx和倒数的每一位数相乘10^i地方i = 0,1,2,...

编辑:感谢@Mauris 3个字节!


你甚至可以做(\b->(x*10^b*).read.pure)
林恩(Lynn)2015年

@毛里斯:很好。非常感谢!
nimi 2015年

1

朱莉娅,50 49字节

f(a,b)=[a*d*10^~-i for(i,d)=enumerate(digits(b))]

该函数接受两个整数并返回一个整数数组。

digits函数以相反的顺序返回输入整数的数组。我们得到索引,值对,enumerate并使用乘积并计算部分乘积,将其作为第一个输入乘以数字乘以10即可得到数字的指数乘方-1。

感谢Dennis,节省了一个字节!


1

Python 2,61

def p(a,b):
 j=0
 while b>0:
  print`b%10*a`+j*'0';b/=10;j+=1 

1

CJam,19个 17字节

q~W%eef{~~A@#**}p

接受输入,第一项是整数,第二项是字符串(例如 34 "53")。欢迎提出建议,因为我敢肯定它可以缩短。感谢Dennis节省了两个字节。

在线尝试。

说明

q~    e# Get input and evaluate it, x and "y"
W%    e# Reverse y so it will be properly sorted
ee    e# Enumerate through y with each character and its index
f{    e# For each digit in y...
  ~~  e# Convert the digit to an integer on the stack
  A@# e# Take 10 to the power of y's index
  **  e# Multiply all three together to get the final result
}
p     e# Print the array

1
~~A@#**保存几个字节。
丹尼斯

1

Haskell,37个字节

a%0=[]
a%b=b`mod`10*a:(a*10)%div b 10

不用算数,只是算术。将最小的部分乘积递归地添加到其余的乘积中,最后的位数b被截断,并且乘数为10。运算符优先级工作良好。


0

𝔼𝕊𝕄𝕚𝕟,11个字符/ 23个字节(非竞争性)

ᴙíⓢⓜî*$*Ⅹⁿ_

Try it here (Firefox only).

在为该问题的解决方案编码时发现了一个错误...

说明

          // Implicit: î = input 1, í = input 2
ᴙíⓢ      // reverse í and split it into an array
ⓜî*$*Ⅹⁿ_ // multiply î by each individual digit in í and put in previous array
          // implicit output

0

Japt,28个字节

I=[]Vs w m@IpApY+1 /A*U*X};I

说明:

I=[]Vs w m@IpApY+1 /A*U*X};I
I=[]                         //that's simple, init an array I
    Vs                       //take the second input and convert to string
       w                     //reverse it and...
         m@              }   //...map through the chars; X is a char, Y is counter
           Ip............    //push the following value into I...
             ApY+1 /A*U*X    //10^(Y+1)/10*U*X, U is the first input
                           I //output the resulting array

由于翻译错误,必须使用ApY+1 /10替代ApY,因为Ap0(这是10 ^ 0)给出了100.我想这是对的原因,以便与快速平方Ap,但0并不意味着“不争论”。请修复,Eth。
nicael

0

Python 2,42个字节

f=lambda a,b:b*[0]and[b%10*a]+f(a*10,b/10)

不用算数,只是算术。递归地将最小的部分乘积附加到其余部分,最后的部分b被截断,并且乘数为10。

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.