文章标题编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
来源:互联网 发布:linux服务器配置怎么看 编辑:程序博客网 时间:2024/06/05 11:11
//编写一个在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); } } }}
阅读全文
0 0
- 文章标题编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是 100 的程序,并输出所有的可能性。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在1,2,…,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 + 34 – 5 + 67 – 8 + 9 = 100
- 用java编写在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100
- (scala实现) 在1到9(顺序不能变)之间插入+或-或什么都不插入,使得计算结果总是100
- 增加一个复选框到上一个程序中使得选中或不选中复选框时插入的文字不同
- 编写程序输出1到100之间出现数字9的所有数字
- 在1,2,…,9(保持这个顺序)之间可任意放+或-或都不放使其结果等于100
- 编写程序,用一个 for 循环计算1+3+5+7 + ……+99的值,并输出计算结果。
- Python的enumerate 枚举函数如何使用?
- Python的编码声明 # -*- coding:utf-8 -*-
- 最小生成树 kruskal算法 C++实现
- 接口回调
- 百练 兔子与樱花(floyd+打印路径)
- 文章标题编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 58笔试题
- intel MKL GEMM
- 《学习opencv》笔记——矩阵和图像操作——cvMax,cvMaxS,cvMerge,cvMin and cvMinS
- 前缀、中缀、后缀表达式
- 自然语言处理---深度学习在情感分析中的应用
- 金蝶K3销售订单客户选择页面禁止显示未审核客户
- 单片机电子电路中常用的负压产生电路
- Ubuntu 解决E: 无法获得锁 /var/lib/dpkg/lock