首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >三重平衡数

三重平衡数
EN

Code Golf用户
提问于 2017-07-17 16:38:58
回答 9查看 930关注 0票数 17

描述

我们考虑一个至少有3位数的整数,如果将每个部分的数字分成三个部分,则每个部分的数字之和等于相同的数。我们将数字除以如下:

代码语言:javascript
复制
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位数的整数,确定给定的数字是否是三重平衡的,并根据其结果输出一个真实或错误的值。

测试用例

代码语言:javascript
复制
333 -> True
343 -> False
3123 -> True
34725 -> True
456456 -> False
123222321 -> True

这是密码-高尔夫,所以标准的漏洞适用,并可能以字节为单位的最短答案获胜!

EN

回答 9

Code Golf用户

发布于 2017-07-17 17:32:21

果冻,23字节

代码语言:javascript
复制
DµL‘:3‘Ṭ©œṗ⁸U®œṗЀS€€FE

在网上试试!

一定有更短的路飞过我的头顶..。

票数 3
EN

Code Golf用户

发布于 2017-07-17 23:01:37

果冻,20字节

代码语言:javascript
复制
DµL‘:3x2jSạ¥¥LRṁ@ḅ1E

在网上试试!

是如何工作的

代码语言:javascript
复制
Dµ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.
票数 3
EN

Code Golf用户

发布于 2017-07-17 21:57:41

Mathematica,142个字节

代码语言:javascript
复制
(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)&
票数 2
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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