如何蛮力算术谜题? [英] How to brute force arithmetic puzzle?
问题描述
一个朋友共享这个难题:
A friend shared this puzzle:
如何使21从数字1,5,6,7?
How to make 21 from the numbers 1, 5, 6, 7?
可以使用的加,减,乘,除,以及括号中的操作。您必须使用每个数字一次。
You can use the operations of addition, subtraction, multiplication and division, as well as brackets. You must use each number once.
我终于在纸面上,两天后解决了这个问题。毫无疑问,一台电脑可以蛮力所有的解决方案在第二。
I eventually solved it on paper—two days later. No doubt a computer could brute force all solutions in a second.
虽然如何?我尝试生成的所有字符串 ABCD
插入信件和运营点的数字,但它错过了我的解决方案。
How though? I tried generating all strings a.b.c.d
inserting the numbers for letters and operations for dots, but it missed my solution.
奖励谜题:
- 如何从1,5,6,7使11?
- 如何从1,5,6,7使16?
推荐答案
这是显而易见的方法是folllwing:
An obvious approach would be the folllwing:
- 您开始用四个数字向量
取值
:S =(1,5,6,7)
- 选择任意两个数字
A
和B
从取值
,从取值
- 在应用的任意算术运算
A
和B
,从而获得新的号码ç
(注意避免被零除和验证整除,如果问题需要它) - 包含
C
到取值
,从而获得S'
- 从第1步到位
的S继续,现在用
S'
- You start with a vector
S
of four numbers:S = ( 1, 5, 6, 7 )
- Pick any two numbers
a
andb
fromS
, remove them fromS
- Apply an arbitrary arithmetic operation to
a
andb
, thus obtaining a new numberc
(take care to avoid division by zero and verify exact division, if the problem requires it) - Include
c
intoS
, thus obtainingS'
- Continue from step 1, now using
S'
in place ofS
蛮力分支在步骤2中执行(选择两个数),和步骤3(选择操作)。该循环应该被重复,直到小号
降低到仅1个元素,这是结果对这个特定蛮力分支。
Brute-force branching is performed at step 2 (selecting two numbers), and step 3 (selecting operation). The cycle should be repeated until S
reduces to only 1 element, which is the result for this particular brute-force branch.
支架没有明确使用,但它们是present隐
Brackets are not used explicitly, but they are present implicitly.
如果一个分支与 21
在结束小号
,你有一个解决方案,此时您可以终止或继续分支搜索其他的解决方案。
If one branch ends with 21
in S
, you have a solution, at which point you can either terminate, or continue branching to search for other solutions.
这篇关于如何蛮力算术谜题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!