你的一个熟人有一种嗜好,就是用各种类型的电池制作换档电器。然而,由于他们节俭,他们希望在他们的项目中使用尽可能少的电池。此挑战使用几种类型的电池;您的工作是输出最少数量的电池,当链接在一起时将输出给定的电压。
以下是您将要使用的电池类型,以及它们的ID号(我为这次挑战指定了这些号码)及其输出电压:
电池也可以一半充电,这意味着它们的电压是完全充电的一半(我已经移除了所有电池的一半电压):
i
必须是1 ≤ i ≤ 100
所在的浮点数,而i
必须有1或2个小数位。下面是一些测试用例(由于运行大数代码所需的时间,数字现在并不大):
input: 1.5
output: [1.5]
input: 2
output: [] or None
input: 7.5
output: [6, 1.5]
input: 15
output: [12, 3]
这是一个密码-高尔夫挑战,所以最少的字节获胜!
发布于 2022-03-28 18:40:03
f=(v,s=[],...y)=>s[v*7|0]?'':eval(s)+v?f(v,...y,...[1.5,3,3.7,9,12,.75,1.85,4.5,6].map(e=>s+-e)):s
-
分裂的回波电压
f=(v,s=[],...y)=> // s,...y as testing buffer
s[v*7|0]?'': // The longest outputLength/input case, -0.75, need *6.67
eval(s)+v? // not equal?
f( // continue
v,
...y,
...[1.5,3,3.7,9,12,.75,1.85,4.5,6].map(e=>s+-e))
:s
发布于 2022-03-28 21:27:02
•UyмµñFb₃^•₅в20/₃иæé.ΔOQ
输出电池的电压列表,如果找不到结果,则输出-1
。
(不要)在网上试试. (所有测试用例都会超时)。
如果我们删除é
,它可以进行一些测试,但它将不再输出所需的最短结果:在网上试试。(而且可能还会超时。)
只有•UyмµñFb₃^•₅в20/₃иæ
,我们可以看到所有可能的powerset列表:在网上试试。
•UyмµñFb₃^• # Push compressed integer 540570253786318174620
₅в # Convert it to base-255 as list: [30,60,74,180,240,15,37,90,120]
20/ # Divide each by 20: [1.5,3,3.7,9,12,0.75,1.85,4.5,6]
₃и # Cycle-repeat this list 95 times
# (95 is the lowest 1-byte constant above 40, which is required to
# reach the maximum to support input 30 with the lowest voltage 0.75)
æ # Pop and push its powerset
é # Sort it by length
.Δ # Find the first inner list which is truthy for:
# (or -1 if none were truthy)
O # Sum the list
Q # Check if it's equal to the (implicit) input-float
# (after which the result is output implicitly)
参见这个05AB1E技巧(章节)如何压缩大整数?和如何压缩整数列表?)来理解为什么•UyмµñFb₃^•
是540570253786318174620
而•UyмµñFb₃^•₅в
是[30,60,74,180,240,15,37,90,120]
。
发布于 2022-03-29 09:11:09
递归蛮力解。
如果找不到解决方案,则抛出异常。
lambda n:(g:=lambda i,s=0,l=[]:[l]*(s==i)if s>=i else[b[0]for a in[12,9,6,4.5,3.7,3,1.85,1.5,.75]if(b:=g(i,s+a,l+[a]))])(n)[0]
https://codegolf.stackexchange.com/questions/245671
复制相似问题