给定一个正整数,输出一个真实/虚假的值,说明这个数字是否可以吃掉自己。
最左边是头,最右边是尾巴。
如果头部大于或等于尾巴,头部就会吃掉尾巴,新的头部就变成它们的总和。
如果是sum \ge 10 ,那么头部将被sum \mod 10替换。
sum=0不能被忽略,但是输入号永远不会有任何前导零。
number=2632
head-2, tail-2
2632 -> 463
head-4, tail-3
463 -> 76
head-7, tail-6
76 -> 3
If only one digit remains in the end, the number can eat itself.如果在任何时候,头部不能吃尾巴,答案将是错误的。
number=6724
072
False (0<2)True:
[2632, 92258, 60282, 38410,3210, 2302, 2742, 8628, 6793, 1, 2, 10, 100, 55, 121]
False:
[6724, 47, 472, 60247, 33265, 79350, 83147, 93101, 57088, 69513, 62738, 54754, 23931, 7164, 5289, 3435, 3949, 8630, 5018, 6715, 340, 2194]这是密码-高尔夫所以最短的代码获胜。
发布于 2018-12-07 14:09:42
Ṛṙ-µṖÄ%⁵:ḊẠṚṙ-µṖÄ%⁵:ḊẠ Main link. Argument: n
Ṛ Reverse n, after casting it to a digit list.
ṙ- Rotate the result -1 units to the left, i.e., 1 unit to the right.
Let's call the resulting digit list D.
µ Begin a new chain with argument D.
Ṗ Pop; remove the last digit.
Ä Accumulate; take the cumulative sum of the remaining digits.
%⁵ Take the sums modulo 10.
Ḋ Dequeue; yield D without its first digit.
: Perform integer division between the results to both sides.
Integer division is truthy iff greater-or-equal is truthy.
Ạ All; return 1 if all quotients are truthy, 0 if not.发布于 2018-12-07 12:16:44
发布于 2018-12-07 08:19:57
0(IntegerDigits@#//.{a_,r___,b_}/;a>=b:>{Mod[a+b,10],r})=={0}&首先对输入调用IntegerDigits以获取其数字列表,然后重复应用以下规则:
{a_,r___,b_} (* match the first digit, 0 or more medial digits, and the last digit... *)
/;a>=b (* under the condition that the number is edible... *)
:>{Mod[a+b,10],r} (* and replace it with the next iteration *)该规则被应用到模式不再匹配为止,在这种情况下,要么只剩下一个数字(真实),要么头部小于尾部(falsy)。
与调用Length[__]==1不同,我们可以使用0(__)=={0}保存几个字节,将列表中的所有元素乘以0,然后与list {0}进行比较。
https://codegolf.stackexchange.com/questions/177118
复制相似问题