package com.gch.test02;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Random;import java.util.Set;import java.util.TreeSet;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;/** * 编写一个在1、2、3。。。9(顺序不能变)数字之间插入+或- 或者什么都不插入,使得计算结果总是100的程序 * 并+输出所有的可能性。例如:1+2+34-5+67-8+9=100 -@author 华 * */public class Test02 { static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript"); public static void main(String[] args) { Set set = new TreeSet(); int num=10000;//随机次数 while(true){ num--; StringBuffer sb = new StringBuffer(); sb.append("1").append(g()); sb.append("2").append(g()); sb.append("3").append(g()); sb.append("4").append(g()); sb.append("5").append(g()); sb.append("6").append(g()); sb.append("7").append(g()); sb.append("8").append(g()); sb.append("9"); int sum = 0; try { Object o = jse.eval(sb.toString()); double d_num = Double.parseDouble(o.toString()); if (d_num > 99 || d_num < 101) { sum = (int) d_num; } } catch (Exception t) { continue; } if (sum == 100) { String string =sb.toString() + "=100"; if(!set.contains(string)){ set.add(string); } } sb.delete(0, sb.length()); if(num==0){ Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } break; } } } public static String g() { int num = new Random().nextInt(3); return num == 0 ? "+" : (num == 1 ? "-" : ""); }}
==========================================================
package com.gch.test02;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;/** * 编写一个在1、2、3。。。9(顺序不能变)数字之间插入+或- 或者什么都不插入,使得计算结果总是100的程序 * 并+输出所有的可能性。例如:1+2+34-5+67-8+9=100 -@author 华 * */public class Test03 { static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript"); public static void main(String[] args) { // 组装字符串所有可能的字符串 StringBuffer sb = new StringBuffer(""); for (int a1 = 0; a1 < 3; a1++) { for (int a2 = 0; a2 < 3; a2++) { for (int a3 = 0; a3 < 3; a3++) { for (int a4 = 0; a4 < 3; a4++) { for (int a5 = 0; a5 < 3; a5++) { for (int a6 = 0; a6 < 3; a6++) { for (int a7 = 0; a7 < 3; a7++) { for (int a8 = 0; a8 < 3; a8++) { // =======================1==================== sb.append("1"); sb.append(r_p(a1)); // =======================2==================== sb.append("2"); sb.append(r_p(a2)); // =======================3==================== sb.append("3"); sb.append(r_p(a3)); // =======================4==================== sb.append("4"); sb.append(r_p(a4)); // =======================5==================== sb.append("5"); sb.append(r_p(a5)); // =======================6==================== sb.append("6"); sb.append(r_p(a6)); // =======================7==================== sb.append("7"); sb.append(r_p(a7)); // =======================8==================== sb.append("8"); sb.append(r_p(a8)); // =======================9==================== sb.append("9"); int sum = 0; try { Object o = jse.eval(sb.toString()); double d_num = Double.parseDouble(o.toString()); if (d_num > 99 || d_num < 101) { sum = (int) d_num; } } catch (Exception t) { continue; } if (sum == 100) { System.out.println(sb.toString() + "=100"); } sb.delete(0, sb.length()); } } } } } } } } } public static String r_p(int num){ String str=""; if (num == 0) { str="+"; } else if (num == 1) { str="-"; } return str; } }
=================================================================================
import java.util.ArrayList;import java.util.List;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;/** * 编写一个在1、2、3。。。9(顺序不能变)数字之间插入+或- 或者什么都不插入,使得计算结果总是100的程序 * 并+输出所有的可能性。例如:1+2+34-5+67-8+9=100 -@author 华 * 18种 123-45-67+89=100 123-4-5-6-7+8-9=100 123+45-67+8-9=100 123+4-5+67-89=100 12-3-4+5-6+7+89=100 12+34-5+67-8=100 12+3-4+5+67+8+9=100 12+3-4+5+6+78=100 12+3+4+5-6-7+89=100 1-2-3+45+67-8=100 1-2+34-5-6+78=100 1+23-4+56+7+8+9=100 1+23-4+5+67+8=100 1+23-4+5+6+78-9=100 1+23+4+5+67=100 1+2+34-5+67-8+9=100 1+2+34+56+7=100 1+2+3-4+5+6+78+9=100 */public class Test04 { static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript"); public static void main(String[] args) { List list=new ArrayList(); int num_=Integer.parseInt(Three2Ten("22222222")); for (int j = 0; j <= num_; j++) { String t2t=Ten2Three(j); StringBuffer sb=new StringBuffer(); String[] lable=new String[9];//012 String[] num={"1","2","3","4","5","6","7","8","9"};//1-9 lable=t2t.split(""); for(int i=0;i<9;i++){ sb.append(num[i]); if(i<8){ if(lable[i+1].equals("0")){ sb.append(""); }else if(lable[i+1].equals("1")){ sb.append("-"); }else{ sb.append("+"); } } int sum = 0; try { Object o = jse.eval(sb.toString()); double d_num = Double.parseDouble(o.toString()); if (d_num > 99 || d_num < 101) { sum = (int) d_num; } } catch (Exception t) { continue; } if (sum == 100) { String s=sb.toString() + "=100"; if(!list.contains(s)){ list.add(s); } } } } System.out.println(list.toString()); } /* * 定义十进制转三进制 */ public static String Ten2Three(int i){ int[] x = new int[8]; if (i >= 0 && i <= 1000000000) { for (int j = 0; j < x.length; j++) { x[j] = i % 3; i = i / 3; } }else { System.out.println("The number is wrong!"); } return toStr(x); } /* * 定义三进制转十进制 */ public static String Three2Ten(String str){ String[] array=new String[9]; array=str.split(""); int sum=0; for(int i=1;i<9;i++){ String si=array[i]; int int_=Integer.parseInt(si); double pow_sum =Math.pow(3,8-i); sum+=int_*pow_sum; //System.out.println(i+"---------------si:"+si+"---------------int_:"+int_+"---------------pow_sum:"+pow_sum+"---------------sum:"+sum); } return sum+""; } /* * 逆序组装输出数组 */ public static String toStr(int[] ii) { StringBuffer sb=new StringBuffer(); for (int j = ii.length-1; j >= 0; j--) { sb.append(ii[j]); } return sb.toString(); }}