如何蛮力算术谜题? [英] How to brute force arithmetic puzzle?

查看:126
本文介绍了如何蛮力算术谜题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个朋友共享这个难题:

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:

  1. 您开始用四个数字向量取值 S =(1,5,6,7)
  2. 选择任意两个数字 A B 取值,从取值
  3. 在应用的任意算术运算 A B ,从而获得新的号码 ç(注意避免被零除和验证整除,如果问题需要它)
  4. 包含 C 取值,从而获得 S'
  5. 从第1步到位的S继续,现在用 S'
  1. You start with a vector S of four numbers: S = ( 1, 5, 6, 7 )
  2. Pick any two numbers a and b from S, remove them from S
  3. Apply an arbitrary arithmetic operation to a and b, thus obtaining a new number c (take care to avoid division by zero and verify exact division, if the problem requires it)
  4. Include c into S, thus obtaining S'
  5. Continue from step 1, now using S' in place of S

蛮力分支在步骤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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆