我为您做了一些调查。
我找到了这个StackExchange问题,介绍了如何创建给定字符集的所有可能组合。您可以使用此方法生成+、-、/、*的所有可能组合,方法在该问题中有描述。
public static void combinations(int maxLength, char[] alphabet, String curr) {
if (curr.length() == maxLength) {
System.out.println(curr);
} else {
for (int i = 0; i < alphabet.length; i++) {
String oldCurr = curr;
curr += alphabet[i];
combinations(maxLength, alphabet, curr);
curr = oldCurr;
}
}
}
并且使用类似以下方式进行调用
char[] operators = new char[]{'+', '-', '/', '*'};
combinations(3, operators , "");
在打印出新组合(
System.out.println(curr);
)之后(或在其替代),你可以调用一个方法来解释输出。可以参考类似于这个
stackoverflow简单计算器示例的东西。
例如,可以使用如下内容:
```java
interpretOutput(curr);
```
其中,`interpretOutput`是一个自定义的方法,在方法中对`curr`进行解释。
if (curr.charAt(0) == '+') {
}
我认为这已经足够让你开始了。
手头有时间,而且觉得这很有趣,所以想要完成它。给你。这完全符合你的需求。
import java.util.Random;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Blah {
public static void main(String[] args) throws ScriptException {
char[] operators = new char[]{'+', '-', '/', '*'};
combinations(3, operators, "");
}
public static void combinations(
int maxLength, char[] alphabet, String curr)
throws ScriptException {
if (curr.length() == maxLength) {
System.out.println(curr);
calculate(curr);
} else {
for (int i = 0; i < alphabet.length; i++) {
String oldCurr = curr;
curr += alphabet[i];
combinations(maxLength, alphabet, curr);
curr = oldCurr;
}
}
}
private static void calculate(String operators) throws ScriptException {
int operand1, operand2, operand3, operand4;
Random randGen = new Random();
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
int result = 0;
String operation = "";
while (result != 20) {
operand1 = randGen.nextInt(101);
operand2 = randGen.nextInt(101);
operand3 = randGen.nextInt(101);
operand4 = randGen.nextInt(101);
operation = String.valueOf(operand1) + operators.charAt(0)
+ String.valueOf(operand2) + operators.charAt(1)
+ String.valueOf(operand3) + operators.charAt(2)
+ String.valueOf(operand4);
result = (int) Double.parseDouble(engine.eval(operation).toString());
}
System.out.println(operation + "= 20");
}
}
为了增加趣味性,我使用了随机数。不确定您如何获取数字输入。需要记住的一件事是,在处理整数时,除法中的小数部分会被舍去。因此,像12/64 = 0或11/5 = 2这样的运算结果都是整数。
以下是一个示例输出:
run:
+++
0+0+10+10= 20
++-
59+3+38-80= 20
++/
19+0+66/53= 20
++*
15+5+0*54= 20
+-+
23+26-97+68= 20
+--
87+66-73-60= 20
+-/
15+5-0/33= 20
+-*
63+57-50*2= 20
+/+
8+61/37+11= 20
+/-
72+38/55-52= 20
+
20+61/71/8= 20
+
20+8*5/54= 20
+**
20+91*0*2= 20
-++
37-100+17+66= 20
-+-
65-89+46-2= 20
-+/
52-32+33/84= 20
-+*
52-32+44*0= 20
--+
39-74-22+77= 20
---
92-0-54-18= 20
--/
62-41-45/73= 20
--*
54-34-0*82= 20
-/+
22-98/9+9= 20
-/-
66-27/71-45= 20
-
20-0/17/41= 20
-
40-75*24/90= 20
-**
20-0*26*90= 20
/++
65/47+6+13= 20
/+-
91/5+56-54= 20
/+/
64/4+69/16= 20
/+*
54/81+2*10= 20
/-+
80/89-68+88= 20
/--
65/2-11-1= 20
/-/
86/4-96/98= 20
/-*
80/4-0*100= 20
12/64/57+20= 20
64/1/1-44= 20
82/1/4/1= 20
45/7/18*57= 20
87/6*55/38= 20
+
7*8/56+19= 20
*/-
38*77/55-33= 20
*
95*99/6/77= 20
*
29*59*1/82= 20
***
4*1*5*1= 20
BUILD SUCCESSFUL (total time: 34 minutes 35 seconds)
由于100个随机整数的限制,像///和***这样的非常棘手的问题使得运行需要34分钟。此外,我没有尽可能地使其更加清洁/高效,并且也没有注意对象泄漏/不必要的对象创建。这将由您来完成。
528 / 14 * 7 / 11
是一个有效的解决方案吗?或者应该像整数一样528 / 14 == 38
? - Andreas