给定长度的幻数


13

您的程序必须接受输入(n出于描述目的)并输出n数字长的数字的所有排列,且没有重复的数字,其中位于其索引之前和包括其索引的每个数字都可以通过其所属数字的位置来整除。 。

您可以在此处阅读有关幻数的信息

规则:

  • 1 <= n <= 10
  • 不能重复数字
  • 前0必须存在(如果适用)
  • x数字的第1至第一个数字(从第一个字符开始为1)必须可被整除x,即,在中306853可被1 30整除,被2 306整除,被3 3068整除,被4整除,并且30685被5 整除。
  • 程序必须采用整数作为输入(通过命令行,作为函数参数等),并打印所有满足规则的排列。
  • 输出必须用1个或多个空格字符分隔
  • 排列可能从零开始(因此从技术上讲不是魔术数字)。
  • 输出顺序无关紧要
  • 不会需要处理突发输入
  • 以字节为单位的最少字符获胜

例子

给定1:

0
1
2
3
4
5
6
7
8
9

给定2:

02
04
06
08
10
12
14
16
18
20
24
26
28
30
32
34
36
38
40
42
46
48
50
52
54
56
58
60
62
64
68
70
72
74
76
78
80
82
84
86
90
92
94
96
98

鉴于10:

3816547290

感谢Pizza Hut和John H. Conway提供的原始拼图(选项A)。感谢@Mego和@ sp3000的链接



6
@DavisDude“相关”并不意味着“重复”。发布相关链接的目的是使该挑战在边栏中显示为“已链接”。
Martin Ender

1
相关阅读:
除数

3
是否需要将包含前导0的输出数字包括在内?
xnor

4
您提到了输出时的打印空格,但是对于函数而言,最自然的输出形式可能是返回列表。可以吗
丹尼斯

Answers:


4

果冻20 17 16 字节

QḣQV%S
ØDṗçÐḟRj⁷

非常慢且占用大量内存... 在线尝试!

怎么运行的

ØDṗçÐḟRj⁷  Main link. Input: n (integer)

ØD         Yield d := '0123456789'.
  ṗ        Compute the nth Cartesian power of d.
      R    Range; yield [1, ..., n].
    Ðḟ     Filter false; keep strings of digits for which the following yields 0.
   ç         Apply the helper link to each digit string and the range to the right.
       j⁷  Join the kept strings, separating by linefeeds.


QḣQḌ%S     Helper link. Arguments: s (digit string), r (range from 1 to n)

Q          Unique; deduplicate s.
 ḣ         Head; get the prefixes of length 1, ..., n or less.
           If s had duplicates, the final prefixes fill be equal to each other.
  Q        Unique; deduplicate the array of prefixes.
   V       Eval all prefixes.
    %      Compute the residues of the kth prefixes modulo k.
           If s and the array of prefixes have different lengths (i.e., if the
           digits are not unique), some right arguments of % won't have corr. left
           arguments. In this case, % is not applied, and the unaltered right
           argument is the (positive) result.
     S     Add all residues/indices. This sum is zero iff all digits are unique
           and the kth prefixes are divisible by k.

3
如果这很慢...我的回答是一个沉睡的
Luis Mendo

6

JavaScript(Firefox 30-57),77个字节

f=n=>n?[for(s of f(n-1))for(c of"0123456789")if(s.search(c)+(s+c)%n<0)s+c]:[""]

编辑:由于@ edc65,节省了1个字节。


一颗宝石!只需保存1个字节即可...of"012...
edc65 '16

@ edc65 gh,我无法相信我忽略了这一点。
Neil 2016年

3

Pyth,19个字节

jf!s%VsM._TS;.PjkUT

示范

蛮力解决方案。解释如下。灵感来自FryAmTheEggman


22字节

juf!%sThH{I#sm+LdTGQ]k

示范

数字被构建并存储为字符串,并且仅转换为int以检查可除性。

说明:

juf!%sThH{I#sm+LdTGQ]k
 u                 Q]k    Apply the following input many times, starting with ['']
             m    G       For each string at the previous step,
              +LdT        Append each digit to it
            s             Concatenate
         {I#              Filter out strings with repeats
  f                       Filter on
     sT                   The integer
    %  hH                 Mod the 1 indexed iteration number
   !                      Is zero.
j                         Join on newlines.

我很好奇:学习Pyth到底有多受虐?/ s
DavisDude 2016年

2
@DavisDude我认为这比人们看到它们时想的要容易。最可怕的部分开始了。一旦您进入,就可以进入
。– FliiFe

1
恕我直言,这很容易,因为调试模式对您有很大帮助。该文档也很好,并解释了您需要了解的内容。
2016年

仅供参考,我总结了更多的使用方法._和其他一些方法,但是对于大量输入内容,它的速度确实会变慢:jjLkf!s.e%ib10hk._T.PUT
FryAmTheEggman

3

MATL,30字节

4Y2Z^!"@Sd@!U10G:q^/kPG:\~h?@!

在线尝试!

非常慢 因为input 3在在线编译器中需要花费几秒钟。要查看数字一一出现,请在代码末尾加aD

说明

4Y2       % predefined literal: string '0123456789'
Z^        % implicit input. Cartesian power: 2D char array. Each number is a row
!         % transpose
"         % for each column
  @       %   push current column
  Sd      %   sort and compute consecutive differences (*)
  @!U     %   push current column. Convert to number
  10G:q^  %   array [1 10 100 ... 10^(n-1)], where n is the input
  /k      %   divide element-wise. Round down
  P       %   reverse array
  G:      %   array [1 2 ... n]
  \~      %   modulo operation, element-wise. Negate: gives 1 if divisible (**)
  h       %   concatenate (*) and (**). Truthy if all elements are nonzero
  ?       %   if so
    @!    %     current number as a row array of char (string)
          %   implicitly end if
          % implicitly end if
          % implicitly display stack contents

您的代码出了点问题;5之后,它将停止为我生成输出,最后5(我不愿检查的唯一数字)不等于5。986无法被3整除
DavisDude


我认为您误解了提示。看着3我可以看到您有几个指示(例如026)。也将不胜感激的解释
DavisDude

这仍然不起作用-3跳过021、024等。第一个正确的数字是063。–
DavisDude

@DavisDude编辑,现在我更加仔细地阅读了挑战
Luis

1

Ruby,87个字节

基本的递归解决方案。

f=->n,x="",j=1{j>n ?puts(x):([*?0..?9]-x.chars).map{|i|f[n,x+i,j+1]if((x+i).to_i)%j<1}}

如果允许您返回排列列表而不是打印,则为85个字节:

f=->n,x="",j=1{j>n ?x:([*?0..?9]-x.chars).map{|i|f[n,x+i,j+1]if((x+i).to_i)%j<1}-[p]}

1

Python,132个字节

lambda n:[x for x in map(("{:0%s}"%n).format,(range(10**n)))if all(int(x[:i])%i<1and len(set(x))==len(x)for i in range(1,len(x)+1))]

通过摆脱丢弃了26个字节itertools,这要归功于Sp3000,让我意识到我不应该使用它。

通过使用列表filter推导而不是丢弃2个字节,再次感谢Sp3000的提示。

在线尝试:Python 2Python 3

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.