123456789中间任意加+或-结果等于100

看到一个题,说123456789中间任意加+或-,123456789的位置不变,结果等于100,刚开始我就想到了坐座位的问题,在123456789之间有8个空位,能放空,+,-三种情况,我用字符串数组保存数据,遍历了一遍,用递归调用方法打印了一遍

package homework;

import java.util.ArrayList;
import java.util.List;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Ch07Work2 {
	public static int Count=1;//定义数字间符号的坐标
	public static String [] F={"","+","-"};//要插入的符号
public static void main(String[] args) {
	//找出所有123456789插入加减 =100
	//StringBuilder s=new StringBuilder("123456789");
	List<String> sl=new ArrayList<>();
	//("1","","2","","3","","4","","5","","6","","7","","8","","9");
	String[] s=new String[]{"1","","2","","3","","4","","5","","6","","7","","8","","9"};
	fuhao(s);
	/*for(int a= 0;a<3;a++)  这个是最笨的,套了8层循环
	 {
		 s[Count]=F[a];
			Count+=2;
			for(int b= 0;b<3;b++)
			 {
				 s[Count]=F[b];
					Count+=2;
					for(int c= 0;c<3;c++)
					 {
						 s[Count]=F[c];
							Count+=2;
							for(int d= 0;d<3;d++)
							 {
								 s[Count]=F[d];
									Count+=2;
									for(int e= 0;e<3;e++)
									 {
										 s[Count]=F[e];
											Count+=2;
											for(int f= 0;f<3;f++)
											 {
												 s[Count]=F[f];
													Count+=2;
													for(int g= 0;g<3;g++)
													 {
														 s[Count]=F[g];
															Count+=2;
															for(int h= 0;h<3;h++)
															 {
																 s[Count]=F[h];
																	Count+=2;
																	String res=tostring(s);
																	
																	ScriptEngineManager manager = new ScriptEngineManager();
																	ScriptEngine engine = manager.getEngineByName("js");
																	Integer result;
																	try {
																		result = (Integer) engine.eval(res);
																		if(result==100)
																		{
																			System.out.println(res+"="+result);
																		}
																	} catch (ScriptException e1) {
																		// TODO Auto-generated catch block
																		e1.printStackTrace();
																	}
																	
																	Count-=2;
																	
															}Count-=2;	
													 }	Count-=2;
											 }	Count-=2;
									 }	Count-=2;
							 }	Count-=2;
					 }	Count-=2;
			 }Count-=2;
	 }*/
	
}
public static String tostring(String[] s){
	 StringBuilder sbd=new StringBuilder();
	 for(String i:s)
	 {
		 sbd.append(i);
	 }
	 return sbd.substring(0);
}
 public static void fuhao(String[] s){//递归方法
	if(Count<=15){
	 for(int a= 0;a<3;a++)
	 {
		 s[Count]=F[a];
			Count+=2;
			fuhao(s);
			Count-=2;
	 }}
	 else{
		 
		 	String res=tostring(s);
			ScriptEngineManager manager = new ScriptEngineManager();
			ScriptEngine engine = manager.getEngineByName("js");
			Integer result;
			try {
				result = (Integer) engine.eval(res);
				if(result==100)
				{
					System.out.println(res+"="+result);
				}
			} catch (ScriptException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
	 }

 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值