文章标题编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
2017-08-22 21:47
751 查看
//编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。 package com.luka; import java.util.ArrayList; public class Javat { private static int TARGET_SUM = 100; private static int[] VALUES = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; private static ArrayList add(int digit, String sign, ArrayList branches) { for (int i = 0; i < branches.size(); i++) { branches.set(i, digit + sign + branches.get(i)); } return branches; } private static ArrayList f(int sum, int number, int index) { int digit = Math.abs(number % 10); if (index >= VALUES.length) { if (sum == number) { ArrayList result = new ArrayList(); result.add(Integer.toString(digit)); return result; } else { return new ArrayList(); } } ArrayList branch1 = f(sum - number, VALUES[index], index + 1); ArrayList branch2 = f(sum - number, -VALUES[index], index + 1); int concatenatedNumber = number >= 0 ? 10 * number + VALUES[index] : 10 * number - VALUES[index]; ArrayList branch3 = f(sum, concatenatedNumber, index + 1); ArrayList results = new ArrayList(); results.addAll(add(digit, "+", branch1)); results.addAll(add(digit, "-", branch2)); results.addAll(add(digit, "", branch3)); return results; } public static void main(String[] args) { ArrayList list = f(TARGET_SUM, VALUES[0], 1); for (int i = 0; i<list.size(); i++) { System.out.println(list.get(i)); } } }
another:
public class Test5{ public static void main(String[] args){ /* 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入, 使得计算结果总是100的程序,并输出所有的可能性。 例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。 */ show(); } public static void show(){ int qian=0,hou=0;//定义运算符连接的两个数分别为qian,hou int ysf=0;//表示运算符 int qh=0;//标记当前的位置,是qian还是hou 0为qian 1为hou int total=0;//算式结果 int[] suan={0,0,0,0,0,0,0,0};////8个可能的运算符插入位,0表没有,1表加法,2表减法 for(suan[0]=0; suan[0]<3; suan[0]++)//1 2间的运算符 for(suan[1]=0; suan[1]<3; suan[1]++)//2 3间的运算符 for(suan[2]=0; suan[2]<3; suan[2]++)//3 4间的运算符 for(suan[3]=0; suan[3]<3; suan[3]++)//4 5间的运算符 for(suan[4]=0; suan[4]<3; suan[4]++)//5 6间的运算符 for(suan[5]=0; suan[5]<3; suan[5]++)//6 7间的运算符 for(suan[6]=0; suan[6]<3; suan[6]++)//7 8间的运算符 for(suan[7]=0; suan[7]<3; suan[7]++){//8 9间的运算符 //重置 qian=1;//第一个数 ysf=0; qh=0; total=0; //开始运算 for(int i=0;i<8;i++){ //无运算符 if(suan[i]==0){ if(qh==0){ qian=qian*10+(i+2); } else hou=hou*10+(i+2); } //遇到了新运算符 else{ //完成前一运算符的结果,结果为qian,依然取hou if(qh==1){ total=qian+ysf*hou; qian=total; hou=i+2; } else{ qh=1; hou=i+2; } //更新运算符 if(suan[i]==1){ ysf=1; } else{ ysf=-1; } } } //完成最后一个运算 total=qian+ysf*hou; //输出 if(total==100){ for(int i=0;i<9;i++){ System.out.print(i+1); if(i<8&&suan[i]==1) System.out.print("+"); if(i<8&&suan[i]==2) System.out.print("-"); } System.out.println("="+total); } } } }
相关文章推荐
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34–5 + 67–8 + 9 = 100。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是 100 的程序,并输出所有的可能性
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是 100 的程序,并输出所有的可能性。 例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100
- 用java编写在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序
- (scala实现) 在1到9(顺序不能变)之间插入+或-或什么都不插入,使得计算结果总是100
- 输出一个整数的每一位,计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,编写程序数一下 1到 100 的所有整数中出现多少次数字 9
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。 输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。 补充说明: 1、操作数为
- 编写程序输出1到100之间出现数字9的所有数字
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2