首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >能自己吃的数字

能自己吃的数字
EN

Code Golf用户
提问于 2018-12-07 07:42:58
回答 11查看 5.9K关注 0票数 31

给定一个正整数,输出一个真实/虚假的值,说明这个数字是否可以吃掉自己。

规则

最左边是头,最右边是尾巴。

如果头部大于或等于尾巴,头部就会吃掉尾巴,新的头部就变成它们的总和。

如果是sum \ge 10 ,那么头部将被sum \mod 10替换。

sum=0不能被忽略,但是输入号永远不会有任何前导零。

示例:

代码语言:javascript
复制
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.

如果在任何时候,头部不能吃尾巴,答案将是错误的。

代码语言:javascript
复制
number=6724
072
False (0<2)

测试用例:

代码语言:javascript
复制
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]

这是密码-高尔夫所以最短的代码获胜。

EN

回答 11

Code Golf用户

发布于 2018-12-07 14:09:42

果冻,11字节

代码语言:javascript
复制
Ṛṙ-µṖÄ%⁵:ḊẠ

在网上试试!

是如何工作的

代码语言:javascript
复制
Ṛṙ-µṖÄ%⁵:ḊẠ  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.
票数 6
EN

Code Golf用户

发布于 2018-12-07 12:16:44

Java (JDK),83字节

代码语言:javascript
复制
n->{int r=0,h=n;while(h>9)h/=10;for(;n>9;h=(h+n)%10,n/=10)r=h<n%10?1:r;return r<1;}

在网上试试!

Credits

票数 5
EN

Code Golf用户

发布于 2018-12-07 08:19:57

Mathematica,62字节

代码语言:javascript
复制
0(IntegerDigits@#//.{a_,r___,b_}/;a>=b:>{Mod[a+b,10],r})=={0}&

首先对输入调用IntegerDigits以获取其数字列表,然后重复应用以下规则:

代码语言:javascript
复制
{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}进行比较。

票数 4
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/177118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档