从一组现有的权重中进行选择以得出目标总和


9

举重时,我想通过将几块板连接到一根杆上来达到特定的重量。

我有以下板块:

  • 6个每块1公斤的盘子
  • 6盘2.5公斤装
  • 6盘5公斤装
  • 6盘10公斤装

酒吧本身重10公斤。

只允许成对安装板-将板连接在钢筋的两端,并且两端的布置必须完全对称(例如,在一端连接两个5公斤重的盘子,在一端连接10公斤重的盘子)出于安全考虑,另一端被禁止)。

编写一个程序或函数,告诉我要获得给定的总重量,我必须使用每种类型的板数。输入为大于11的整数。输出是一个包含4个数字的列表/数组/字符串。如果不可能合并现有板块以获得目标重量,则输出零/空数组,无效字符串,引发异常等。

如果有多种解决方案,则代码只能输出一种(不要让用户选择-他忙于其他事情)。

测试用例:

12 -> [2 0 0 0] - 2 plates of 1 kg plus the bar of 10 kg
13 -> [0 0 0 0] - a special-case output that means "impossible"
20 -> [0 0 2 0] - 2 plates of 5 kg + bar
20 -> [0 4 0 0] - a different acceptable solution for the above
21 -> [6 2 0 0] - 6 plates of 1 kg + 2 plates of 2.5 kg + bar
28 -> [0 0 0 0] - impossible
45 -> [0 2 6 0] - a solution for a random number in range
112 -> [2 4 6 6] - a solution for a random number in range
121 -> [6 6 6 6] - maximal weight for which a solution is possible

如果您的代码以相反的顺序输出数字(从厚板到轻板),请明确指定此编号以避免混淆。


1
这不是对最小硬币计数问题的欺骗吗?我不认为相同的贪婪算法会失败,除了一种板的6个限制之外。我认为这可能还不够,但是我不确定。
FryAmTheEggman'6

1
贪婪算法无法工作(至少在没有修改的情况下),恰恰是因为数量有限
-anatolyg

相关,但其中一个是ASCII
AdmBorkBork

是的,我发布的原因是我不确定修改是否足够重要。我发布帖子是为了获得社区的反馈,如果社区不同意我的意见,我将删除我的评论。
FryAmTheEggman

我们可以输出所有解决方案而不是仅输出一个吗?
路易斯·门多

Answers:


5

果冻,22 字节

4ṗạµ×2,5,10,20S€+⁵iƓịḤ

在线尝试!验证所有测试用例

怎么运行的

4ṗạµ×2,5,10,20S€+⁵iƓịḤ  Main link. No arguments

4                       Set the left argument and initial return value to 4.
 ṗ                      Take the Cartesian power of [1, 2, 3, 4] and 4, i.e.,
                        generate all 4-tuples of integers between 1 and 4.
  ạ                     Take the absolute difference of all integers in the
                        4-tuples and the integer 4. This maps [1, 2, 3, 4] to
                        [3, 2, 1, 0] and places [0, 0, 0, 0] at index 0.
   µ                    Begin a new, monadic chain. Argument: A (list of 4-tuples)
     2,5,10,20          Yield [2, 5, 10, 20].
    ×                   Perform vectorized multiplication with each 4-tuple in A.
              S€        Sum each resulting 4-tuple.
                +⁵      Add 10 to each sum.
                   Ɠ    Read an integer from STDIN.
                  i     Find its first index in the array of sums (0 if not found).
                     Ḥ  Unhalve; yield the list A, with all integers doubled.
                    ị   Retrieve the 4-tuple at the proper index.

6

MATL29 28字节

4t:qEZ^!"[l2.5AX]@Y*10+G=?@.

对于没有解决方案的输入,将产生空输出(无错误)。

在线尝试!

说明

4           % Push 4
t:q         % Duplicate 4 and transform into range [0 1 2 3]
E           % Multiply by 2: transform into [0 2 4 6]
Z^          % Cartesian power. Each row is a "combination" of the four numbers
!           % Transpose
"           % For each column
  [l2.5AX]  %   Push [1 2.5 5 10]
  @         %   Push current column
  Y*        %   Matrix multiply. Gives sum of products
  10+       %   Add 10
  G=        %   Compare with input: are they equal?
  ?         %   If so
    @       %     Push current column, to be displayed
    .       %     Break loop
            %   Implicit end
            % Implicit end
            % Implicit display

5

Mathematica,70个字节

Select[FrobeniusSolve[{2,5,10,20},2#-20],AllTrue[EvenQ@#&&#<7&]][[1]]&

匿名函数。将数字作为输入,然后输出列表或错误,{}[[1]]如果没有解决方案,则返回。


4

果冻,25个字节

×2,5,10,20S+⁵⁼³
4ṗ4’ÇÐfḢḤ

在这里尝试。


2,5,10,20->2,5,⁵,20
Leaky Nun

真的...是不是,一个对子?我的整个人生是一个谎言
漏嫩

@LeakyNun ,是一个二元组,但也可以用于文字。2,5,⁵,20是不是字面虽然(2,520是,但是,,是原子),所以你需要的东西的联系结合起来。
丹尼斯

3

Python 3,112字节

lambda n:[i for i in[[i//4**j%4*2for j in range(4)]for i in range(256)]if i[0]+2.5*i[1]+5*i[2]+10*i[3]+10==n][0]

一个匿名函数,它通过参数输入目标质量,并以列表形式返回每个板的编号。如果不存在解决方案,则会引发错误。这是纯蛮力。

怎么运行的

lambda n                                   Anonymous function with input target mass n
...for i in range(256)                     Loop for all possible arrangement indices i
[i//4**j%4*2for j in range(4)]             Create a base-4 representation of the index i,
                                           and multiply each digit by 2 to map from
                                           (0,1,2,3) to (0,2,4,6)
[...]                                      Package all possible arrangements in a list
...for i in...                             Loop for all possible arrangements i
i...if i[0]+2.5*i[1]+5*i[2]+10*i[3]+10==n  Return i if it gives the target mass
[...]                                      Package all solutions in a list
:...[0]                                    Return the first list element. This removes any
                                           multiple solutions, and throws an error if there
                                           being no solutions results in an empty list

在Ideone上尝试


2

Brachylog,50个字节

,L##l4,L:{.~e[0:3]}a:[2:5:10:20]*+:10+?,L:{:2*.}a.

false不可能时返回。


1

Pyth,34 31 25字节

h + fqQ +; s * VT [1 2.5 5;)yMM ^ U4 4] * 4] 0 
yMh + fqQ +; s * VT [2 5; y;)^ U4 4] * 4] 0
yMhfqQ +; s * VT [2 5; y;)^ U4 4

测试套件。

不可能的错误。

这本质上是蛮力。

这是非常快的,因为只有256种可能的安排。


1

Scala,202字节

决定性的Scala在这里并没有获得太多的关注,因此我在Scala中提出了一个(可能不是最佳的)解决方案。

def w(i:Int){var w=Map(20->0,10->0,5->0,2->0);var x=i-10;while(x>0){
var d=false;for(a<-w.keys)if(a<=x & w(a)<6 & !d){x=x-a;w=w.updated(a,w(a)+2);d=true;}
if(!d){println(0);return;}}
println(w.values);}

与发布后的解决方案相比,该程序以相反的顺序输出并带有额外的垃圾。如果找不到解决方案,则打印0。

注意:由于Scala太笨了,因此我无法删除任何换行符或空格,因此我认为为减小大小,除非我错过了明显的内容,否则必须重做该方法。


1

APL,40个字节

{2×(4⍴4)⊤⍵⍳⍨10+2×,⊃∘.+/↓1 2.5 5 10∘.×⍳4}

在⎕IO←0中。用英语:

  1. 10+2×,∘.+⌿1 2.5 5 10∘.×⍳4:通过计算每种重量类型的重量的4D外部总和,构建所有可能重量的数组;
  2. ⍵⍳⍨:搜索给定的索引。如果未找到,则索引为1+步骤1中数组的计数;否则为0。
  3. (4⍴4)⊤:表示以4为底的索引,即计算给定的weigth在4D空间中的坐标;
  4. :将结果带到问题空间,其中坐标应解释为板数的一半。

示例:{2×(4⍴4)⊤⍵⍳⍨10+ 2×,⊃∘。+ /↓1 2.5 510∘。×⍳4} 112 2 4 6 6

奖励:由于APL是一种数组语言,因此可以一次测试多个权重。在这种情况下,结果将转置:

      {2×(4⍴4)⊤⍵⍳⍨10+2×,⊃∘.+/↓1 2.5 5 10∘.×⍳4}12 13 20 21 28 45 112 121
2 0 0 6 0 0 2 6
0 0 0 2 0 2 4 6
0 0 2 0 0 2 6 6
0 0 0 0 0 2 6 6

1

JavaScript(ES6),109个字节

n=>`000${[...Array(256)].findIndex((_,i)=>i+(i&48)*9+(i&12)*79+(i&3)*639+320==n*32).toString(4)*2}`.slice(-4)

返回00-2错误。undefined错误返回的替代解决方案,也是109个字节:

n=>[...Array(256)].map((_,i)=>`000${i.toString(4)*2}`.slice(-4)).find(s=>+s[0]+s[1]*2.5+s[2]*5+s[3]*10+10==n)
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.