查找数字并计算输出


22

目的

给定的输入列表中6不同的数字,发现3个数字ab并且c使得a × b = c,与a具有2位,b具有1位和c具有3个位数。以一种更直观的方式,您的程序必须在该图像的框中排列这6位数字:

在此处输入图片说明

如果存在多个解决方案,则可以输出其中任何一个。

输入项

6个不同的数字。您可以根据自己的语言以任何合理的方式使用它们。

输出量

3号abc。只要将3个数字分开并始终以相同的顺序打印(但不一定按顺序a, b, c),输出格式就相对自由。

测试用例

1, 2, 3, 4, 5, 6 -> 54,3,162  
2, 3, 4, 5, 6, 7 -> 57,6,342 or 52,7,364

计分

以字节为单位的最短代码获胜。


1
这也是我也看到挑战时想到的第一件事@Dada ...我建议将其放在沙盒中,以便在发布之前获得一些反馈:-)
Stewie Griffin

1
输入是否保证可以解决?
AdmBorkBork

1
我已对您的挑战措词进行了很多编辑,以使其清楚(我认为)。确保我没有改变挑战的目标。
Fatalize

1
我也认为挑战需要更明确的标题,但我现在没有主意。
Fatalize

1
应输入0,1,2,3,4,5结果13,4,052; 没有解决方案; 还是任何行为都可以吗?
乔纳森·艾伦,

Answers:


8

Brachylog(2),10个字节

p~c₃o.k×~t

在线尝试!

太慢了,无法在合理的时间内运行(Brachylog解释器花很长时间使用空速很慢的约束求解器对空字符串,4位数字,负数等进行乘法运算)。TIO链接使用的输入只有3位数字(此程序可以处理任意位数的输入)。此函数的输入是一个包含所有所需数字的数字(例如234567)–输入中没有重复项意味着您始终可以将任何数字都放在0末尾以避免前导零–并且该函数的输出是列表中的列表订单[b, a, c](例如[6, 57, 342])。

说明

p~c₃o.k×~t
p           Permute the digits of the input
 ~c₃        Split them into three groups
    o       Sort the three groups
     .      to produce the output, which must have the following property:
      k     all but the last group
       ×    when multiplied together
        ~t  produces the last group

那么,对组的2位,1位和3位的要求去哪儿了?好吧,我们知道输入中有6位数字,并且这些组按排序顺序排列。因此,它们只能具有的大小为[1、1、4],[1、2、3]或[2、2、2]。第一种情况是不可能的(您不能将两个1位数字相乘以产生4位数字,因为9×9仅是81),最后一种情况是不可能的(您不能将两个2位数字相乘以产生2位数,甚至10×10也产生100)。因此,返回值[b, a, c]必须按该顺序依次为1位,a2位和3位数字,b也就是2位数字,1位数字和c3位数字(根据要求)。


2
好吧...我投降了
致命一战

8

JavaScript(ES6),90 88字节

将输入作为6位数字的数组。返回一个描述可能解决方案的字符串(例如'54*3==162'),或者在(且仅当)没有解决方案的情况下以“太多递归”错误退出。

f=(a,k=1)=>eval(s='01*2==345'.replace(/\d/g,n=>a[n],a.sort(_=>(k=k*2%3779)&2)))?s:f(a,k)

怎么运行的

这是确定性算法。

选择质数P=2Q=3779的方式确保排序回调(k = k * P % Q) & 2可以随时间生成输入数组的所有720个可能的排列。更准确地说,所有排列都在2798种排序后被覆盖-应该在所有浏览器的递归限制之内。

01*2==345通过将数字映射到数组中的相应条目,我们在表达式中注入了每个排列。

我们评估该表达式并进行递归调用,直到它成立为止。

测试


假设输出格式仍然有效,请使用-代替==(并反转?:)以保存一个字节。
尼尔,

1
@Neil实际上,我对齐柏林飞艇提出了相同的建议。我可能还应该打高尔夫球,但是我必须承认我喜欢当前的输出格式。
Arnauld

您是用蛮力找到3379的,还是使用数学推理的?如果是这样,可以提供找到它的方式吗?:)
Yytsi'2

@TuukkaX这里没什么好看的。我只是强加了它,我的标准是1)P和Q尽可能少的数字,2)尽可能少的排序迭代。
Arnauld

6

Brachylog,17个字节

p~c[Ċ,I,Ṫ]cᵐ.k×~t

在线尝试!

说明

p                   Try a permutation of the Input
 ~c[Ċ,I,Ṫ]          Deconcatenate it; the result must be a list of the form [[_,_],_,[_,_,_]]
          cᵐ.       Output is the list of integers you get when mapping concatenate on the
                      previous list
             k×~t   The first two ints of the Output, when multiplied, result in the third
                      int of the Output

3

05AB1E15 13字节

感谢Emigna节省了两个字节!

œJvy3L£Â`*Qi,

使用CP-1252编码。在线尝试!

说明:

œ                 # Get all permutations of the input
 J                # Join them to get the numbers
  vy              # For each element in the list..
    3L            #   Push the list [1, 2, 3]
      £           #   Pops a and pushes [a[0:1], a[1:3], a[3:6]]
       Â`         #   Bifurcate and flatten
         *        #   Multiply the top two elements in the stack
          Qi      #   If equal to the third element..
            ,     #     Print the array

您可以替换为213S3L因为订单不一定2,1,3要符合规格。
Emigna '17

£高兴知道累积向量化...如果这是正确的说法。
魔术章鱼缸

3

Bash + coreutils,70岁

for((b=1;b;));{
a=`shuf -ze $@`
b=${a:0:2}*${a:2:1}-${a:3:3}
}
echo $b

没有特别简单的方法来生成所有排列。而是随机生成排列并进行计算,直到找到一个好的排列。

输出采用以下形式A*B-C-即当我们具有正确的排列时将计算为零的表达式。

在线尝试



2

Python 2,105个字节

lambda s:[(x[0],x[1:3],x[3:])for x in permutations(s)if eval('%s*%s%s==%s%s%s'%x)]
from itertools import*

在线尝试!

88字节解决方案,输出更加灵活

lambda s:[x for x in permutations(s)if eval('%s*%s%s==%s%s%s'%x)]
from itertools import*

在线尝试!
输出为['6','5','7','3','4','2'],而不是“ 6”,“ 57”,“ 342”


2
您没有把自己import
放在首位

@ mbomb007必须在TIO上工作\ _(ツ)_ /¯–
Rod

您是我见过的第一个实际将f=标头放置在标题中的人。没什么大不了的。
mbomb007 '17

2

PHP,110字节

它将到达那里...最终...

<?$v=$argv;unset($v[0]);do shuffle($v)&[$a,$b,$c,$d,$e,$f]=$v;while("$a$b"*$c!="$d$e$f");echo"$a$b $c $d$e$f";

取消高尔夫:

<?
$v=$argv;
unset($v[0]);
do
  shuffle($v);
  [$a,$b,$c,$d,$e,$f]=$v;
while("$a$b"*$c!="$d$e$f");
echo"$a$b $c $d$e$f";

2

PHP,77字节

for(;;)eval(strtr('0.*1-"428"||die("0.,1,428");',1/7,str_shuffle($argv[1])));

将输入作为字符串。


1

ES6(JavaScript), 8582,79字节

接受数字(字符串)数组,返回3元素数组[A,B,C]=> C=A*B

打高尔夫球

R=(w,[a,b,c,d,e,f]=w)=>f*(d+=e)^(a+=b+c)?R(w.sort(_=>Math.random()-.5)):[a,d,f]

编辑:

  • 通过重复使用da,并==省去了3个字节(谢谢@Arnauld!)
  • 使用解构分配节省了3个字节

试试吧 !

R=(w,[a,b,c,d,e,f]=w)=>f*(d+=e)^(a+=b+c)?R(w.sort(_=>Math.random()-.5)):[a,d,f];

function generate(A) {
   console.log(R([...A]));
}
<input type="text" id="A" value="123456"/><button onclick="generate(A.value)">GENERATE</button>


您能保证您的随机排序实际上会覆盖所有排列吗?
尼尔,

@Neil,如果您正在寻找严格的形式证明,我想我不能为您提供证明,但是凭经验,它的确可以使排列的分布非常均匀。
Zeppelin

1

,18字节

17个字节的代码,-S标志+1 。

$/_=1FI_^@3,5MPMa

通过命令行参数以数字字符串形式输入输入。输出按c,b,a的顺序排列。在线尝试!

如果存在多个,此代码将输出所有解决方案。如果仅需要输出一种解决方案,则添加三个字节并将程序包装在中(...0)

说明

                   a is 1st cmdline arg (implicit)
              PMa  Compute all permutations of a
             M     To each, map this function:
          3,5       Range(3,5)--contains values 3 and 4
       _^@          Split the function argument at those indices
                    This transforms a string like 342657 into a list [342; 6; 57]
     FI            Now filter the list of split permutations on this function:
$/_                 Fold on division: takes 1st element and divides it by the rest
   =1               Compare the quotient with 1
                    This keeps only the permutations where the first number is the product
                    of the other two
                   Autoprint the list (implicit), with each sublist on a separate line
                   and space-separated (-S flag)

1

Ruby,60个字节

->x{x.permutation{|a|(eval a="%d%d*%d==%d%d%d"%a)&&puts(a)}}

将所有解决方案打印为“ a * b == c”

例:

->x{x.permutation{|a|(eval a="%d%d*%d==%d%d%d"%a)&&puts(a)}}[[1,2,3,4,5,6]]
54*3==162

->x{x.permutation{|a|(eval a="%d%d*%d==%d%d%d"%a)&&puts(a)}}[[2,3,4,5,6,7]]
52*7==364
57*6==342

1

批次,305个字节

@echo off
set/pd=
for /l %%i in (0,1,719)do set n=%%i&call:c
exit/b
:c
set t=%d%
set s=
for /l %%j in (6,-1,1)do set/ap=n%%%%j,n/=%%j&call:l
set s=%s:~0,2%*%s:~2,1%-%s:~3%
set/an=%s%
if %n%==0 echo %s%
exit/b
:l
call set u=%%t:~%p%%%
call set s=%%s%%%%u:~,1%%
call set t=%%t:~,%p%%%%%u:~1%%

将STDIN上的输入作为字符串[1-9]{6}并以dd*d-ddd格式输出所有解决方案。批处理不是很擅长字符串操作,因此生成720个排列有点尴尬。

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.