今天,在我的统计类中,我发现当相乘在一起时,一些阶乘可以被简化!例如:5! * 3! = 5! *3*2 = 5! *6 = 6!
给定一个只包含阿拉伯数字和感叹号的字符串,将我的阶乘简化为尽可能最短的字符串,以最少的字节为您的语言编写代码高尔夫样式。
只包含阿拉伯数字和感叹号的字符串。输入的阶乘数不会大于200!每个数不超过一个阶乘。输入可以作为整数列表。
一个可能缩短的字符串,它在输入上具有等价的值。秩序并不重要。阶乘表示法是必须的,但每个数字不需要使用一个以上的阶乘符号。
In: 3!2!2!
Out: 4!
In 2!3!2!0!
Out: 4!
In: 7!2!2!7!2!2!2!2!
Out: 8!8!
In: 23!3!2!2!
Out: 24!
Also: 4!!
In: 23!3!2!2!2!
Out: 24!2!
In: 127!2!2!2!2!2!2!2!
Out: 128!
In: 32!56!29!128!
Out: 29!32!56!128!祝你好运
发布于 2018-01-18 20:16:22
,!P€E
SṗLçÐfµḢḟ1ȯ1F又快又脏。非常慢,即使是23!2!3!2!测试用例也是很牵强的。I/O作为整数列表。
,!P€E Helper link. Arguments: attempt, original
, Make the array [attempt, original].
Example: [[1,1,1,4], [2,3,2,0]]
! Take the factorial of each item.
Example: [[1,1,1,24], [2,6,2,1]]
P€ Take the product of each sublist.
Example: [24, 24]
E Check if the values are equal.
SṗLçÐfµḢḟ1ȯ1F Main link. Arguments: original
S Find the sum S of the integers in the input.
L Find the number N of integers in the input.
ṗ Generate all lists containing N integers from 1 to S.
çÐf Take the lists whose factorial-product is the same as the original.
Ḣ Take the first match. This is the one with the most ones.
ḟ1 Remove any ones.
ȯ1 If there were only ones, return a one instead.
F Turn into a list if needed.发布于 2018-01-20 10:46:07
https://codegolf.stackexchange.com/questions/153526
复制相似问题