输入任意的四个数,求出所有能得到二十四点的算式,不过我是菜鸟,可能性能方面不好,希望各位多多指教
?1. [代码][Java]代码
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class T2 {
/**
* 判断一个整数数组中有没有相等的
* @param intvals
* @return
*/
public boolean isEqual(int... intvals){
for (int i = 0; i < intvals.length; i++) {
for (int j = 0; j < intvals.length; j++) {
if(intvals[i]==intvals[j]&&i!=j){
return true;
}
}
}
return false;
}
/**
* 正式计算,这里并没用处理除0错误的情况。因为我觉得就算/0也不会让程序终止,没用必要处理。
* @param engineering
* @param chars
* @param arrs
* @param jieguo
* @throws ScriptException
*/
public void jisuan(ScriptEngine engineering,int jieguo,String[] chars,int...arrs) throws ScriptException{
for (int i = 0; i < chars.length; i++) {
for (int j = 0; j < chars.length; j++) {
for (int j2 = 0; j2 < chars.length; j2++) {
/**海市蜃楼
* 算是默认是没有括号()的
*/http://www.huiyi8.com/haishishenlou/plg/
String[] suanshis={""+arrs[0],chars[i],""+arrs[1],chars[j],""+arrs[2],chars[j2],""+arrs[3]};
runit(engineering, Arrays.asList(suanshis), jieguo);
jiSuanKuoHao(suanshis,jieguo,engineering);
}
}
}
}
/**
*
* @param suanshi
* @param jieguo
* @param engineering
* @throws ScriptException
*/
public void jiSuanKuoHao(Object[] suanshis,int jieguo,ScriptEngine engineering) throws ScriptException{
List<String> temp1=null;
List<String> temp2=null;
for (int i = 0; i < suanshis.length; i++) {
boolean insert=false;
temp1=new ArrayList<String>();
for (int k = 0; k < suanshis.length; k++) {
temp1.add((String)suanshis[k]);
}
if(Pattern.matches("[0-9]*", temp1.get(i))){
if(i!=suanshis.length-1){
temp1.add(i,"(");
insert=true;
}
}
if(insert){
for (int j = i+2; j < suanshis.length+1; j++) {
temp2=new ArrayList<String>();
for (int k = 0; k < temp1.size(); k++) {
temp2.add(temp1.get(k));
}
if(Pattern.matches("[0-9]*", temp1.get(j))){
temp2.add(j+1,")");
//System.out.println(temp2);
runit(engineering, temp2, jieguo);
if(temp2.indexOf("(")==temp2.lastIndexOf("(")){
jiSuanKuoHao(temp2.toArray(), jieguo, engineering);
}
}
}
}
}
}
/**
*
* @param engineering
* @param string
* @param jieguo
* @throws ScriptException
*/
public void runit(ScriptEngine engineering,List<String> strings,int jieguo) throws ScriptException{
String string="";
for (int i = 0; i < strings.size(); i++) {
string+=strings.get(i);
}
Object object=engineering.eval(string);
String result=object.toString();
if(result!=null&&!"null".equals(result)&&!"Infinity".equals(result)&&!result.equals("NaN")){
double val=Double.parseDouble(result);
if(val==jieguo){
System.out.println(string+"="+val);
}
}
}
/**
*
* @param arr
* @param jieguo
* @param engineering
* @param chars
* @throws ScriptException
*/
public void start(int[] arr, int jieguo,ScriptEngine engineering,String[] strs) throws ScriptException{
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
for (int j2 = 0; j2 < arr.length; j2++) {
for (int k = 0; k < arr.length; k++) {
if(!isEqual(i,j,j2,k)){
jisuan(engineering, jieguo,strs, arr[i],arr[j],arr[j2],arr[k]);
}
}
}
}
}
}
public static void main(String[] args) throws ScriptException{
ScriptEngine engine=new ScriptEngineManager().getEngineByName("js");
String[] chars={"+","-","*","/"};
int[] arr={14,13,5,8};
try {
new T2().start(arr,24,engine, chars);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
?
java24点算法