发布网友 发布时间:2022-04-21 17:58
共1个回答
热心网友 时间:2023-06-16 01:35
灞曞紑鍏ㄩ儴import java.text.MessageFormat;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class T {
private static final String T = "{0}+13*{1}/{2}+{3}+12*{4}-{5}-11+{6}*{7}/{8}-10";
public static void main(String[] args) throws ScriptException {
int[] a = initArray(9);
p(0, a);
}
private static void e(int[] a) throws ScriptException {
String e = MessageFormat.format(T, a[0], a[1], a[2], a[3], a[4], a[5],
a[6], a[7], a[8]);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
Number n = (Number) engine.eval(e);
if (n.intValue() == 66 && n.doubleValue() == n.intValue()) {
System.out.println(e + " = " + 66);
}
}
private static int[] initArray(int n) {
int[] a = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = i + 1;
}
return a;
}
private static void p(int i, int[] a) throws ScriptException {
if (i == a.length - 1) {
e(a);
return;
}
p(i + 1, a);
for (int j = i + 1; j < a.length; j++) {
swap(i, j, a);
p(i + 1, a);
swap(j, i, a);
}
}
private static void swap(int i, int j, int[] a) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}