编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序
2015-05-17 11:58
639 查看
编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序。并输出所有的可能性。
Start
1+2+3-4+5+6+78+9 = 100
1+2+34-5+67-8+9 = 100
1+23-4+5+6+78-9 = 100
1+23-4+56+7+8+9 = 100
12+3+4+5-6-7+89 = 100
12+3-4+5+67+8+9 = 100
12-3-4+5-6+7+89 = 100
123+4-5+67-89 = 100
123+45-67+8-9 = 100
123-4-5-6-7+8-9 = 100
123-45-67+89 = 100
End
/* * 可以看成是在数字中间插入运算符。插入 + - 或空位。那么每个空位有3种选择,加号,减号或者空。 * 一共8个位置,共有3的8次方个情况。每个情况都试一次,找出结果为100的组合。 * */ public class ResultOneHundred { public static void main(String args[]){ String num[] = new String[17]; int count1 = 1; for (int i = 0; i < num.length; i++) { if(i%2 == 0){ num[i] = count1 + ""; count1++; } else{ num[i] = ""; } }//准备好特定格式的数组 System.out.println("Start"); get100(num, 1, 16, num); System.out.println("End"); } /** * 找出结果为100的表达式 * @param data1 * @param count * @param times * @param data2 */ static void get100(String data1[], int count,int times,String data2[]){ if(count >= times){ if (calculate100(data2) == 100){ for (int j = 0; j < data2.length; j++) { System.out.print(data2[j]); } System.out.println( " = 100"); } return; } String temp1[] = new String[data1.length]; String temp2[] = new String[data1.length]; String temp3[] = new String[data1.length]; String add = "+"; String minus = "-"; String empty = ""; copyStringArray(temp1, data2); copyStringArray(temp2, data2); copyStringArray(temp3, data2); temp1[count] = add; temp2[count] = minus; temp3[count] = empty; temp1[count+1] = data1[count+1]; temp2[count+1] = data1[count+1]; temp3[count+1] = data1[count+1]; count+=2; get100(data1, count, times, temp1); get100(data1, count, times, temp2); get100(data1, count, times, temp3); } /** * 复制字符数组的方法,从from[]复制到to[] * @param to * @param from */ static void copyStringArray(String to[], String from[]){ for (int i = 0; i < from.length ; i++) { to[i] = from[i]; } } /** * 求和方法,输入一个String[]运算式子 * 数组要写成指定的格式,大于10的数之间要用""隔开。 * @see ["1","","4"<span style="font-family: Arial, Helvetica, sans-serif;">,""</span>,"7","+","7"] * @param cal * @return 表达式的和 */ static int calculate100(String cal[]){ int sum = 0; int bitCount = 0; int tempNum = 0; for (int i = cal.length - 1; i >= 0; i--) { if (cal[i] == "+") { sum+=tempNum; tempNum = 0; bitCount = 0; } else if (cal[i] == "-"){ sum-=tempNum; tempNum = 0; bitCount = 0; } else if (cal[i] == ""){ bitCount++; } else{ tempNum+=Math.pow(10, bitCount) * Integer.parseInt(cal[i]); } } return sum+=tempNum; } }
Start
1+2+3-4+5+6+78+9 = 100
1+2+34-5+67-8+9 = 100
1+23-4+5+6+78-9 = 100
1+23-4+56+7+8+9 = 100
12+3+4+5-6-7+89 = 100
12+3-4+5+67+8+9 = 100
12-3-4+5-6+7+89 = 100
123+4-5+67-89 = 100
123+45-67+8-9 = 100
123-4-5-6-7+8-9 = 100
123-45-67+89 = 100
End
相关文章推荐
- 编写一个在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,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在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,…,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-9这9个数字按从小到大的顺序排列 ,中间添上“+”和"-"可以计算的结果等于100的程序;
- 把1-9这9个数字按从小到大的顺序排列 ,中间添上“+”和"-","",可以计算的结果等于100的程序
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。 输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。 补充说明: 1、操作数为
- 编写一个java程序,从1加到100,每做一次加法,就休眠1秒,当程序运行过程中,杀死这个程序。 再次运行,还能从上次的计算结果继续下去
- 编写一个java程序,从1加到100,结果是5050。 每做一次加法,就休眠1秒 当程序运行过程中,杀死这个程序。 再次运行,还能从上次的计算结果继续下去。
- 输出一个整数的每一位,计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,编写程序数一下 1到 100 的所有整数中出现多少次数字 9