我们考虑一个至少有3位数的整数,如果将每个部分的数字分成三个部分,则每个部分的数字之和等于相同的数。我们将数字除以如下:
abcdefghi - Standard case: the number of digits is divisable through 3:
abc def ghi
abcdefgh - Number % 3 == 2: The outer groups are both assigned another digit
abc de fgh (the inner group will have one digit less than both outer groups)
abcdefghij - Number % 3 == 1: The inner group is assigned the extra digit
abc defg hij (the inner group will have one digit more than the outer groups)您的任务是编写一个程序,该程序给定至少3位数的整数,确定给定的数字是否是三重平衡的,并根据其结果输出一个真实或错误的值。
333 -> True
343 -> False
3123 -> True
34725 -> True
456456 -> False
123222321 -> True这是密码-高尔夫,所以标准的漏洞适用,并可能以字节为单位的最短答案获胜!
发布于 2017-07-17 23:01:37
DµL‘:3x2jSạ¥¥LRṁ@ḅ1EDµL‘:3x2jSạ¥¥LRṁ@ḅ1E Main link. Argument: n
D Decimal; convert n to base 10, yielding a digits array A.
µ Begin a new chain with argument A.
L Compute the length of A.
‘ Increment; add 1 to the length.
:3 Divide the result by 3.
This yields the lengths of the outer chunks.
x2 Repeat the result twice, creating an array C.
L Compute l, the length of A.
¥ Combine the two links to the left into a dyadic chain.
This chain will be called with arguments C and l.
¥ Combine the two links to the left into a dyadic chain.
S Take the sum of C.
ạ Compute the absolute difference of the sum and l.
j Join C, using the result to the right as separator.
We now have an array of the lengths of all three chunks the
digits of n have to be split in.
R Range; map each chunk length k to [1, ..., k].
ṁ@ Mold swapped; takes the elements of A and give them the shape
of the array to the right, splitting A into chunks of the
computed lengths.
ḅ1 Convert each chunk from unary to integer, computing the sum
of its elements.
E Test if the resulting sums are all equal.发布于 2017-07-17 21:57:41
(s=IntegerDigits@#;c=Floor;If[Mod[t=Length@s,3]==2,a=-1;c=Ceiling,a=Mod[t,3]];Length@Union[Tr/@FoldPairList[TakeDrop,s,{z=c[t/3],z+a,z}]]==1)&https://codegolf.stackexchange.com/questions/132926
复制相似问题