编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34–5 + 67–8 + 9 = 100。
2015-05-14 10:16
786 查看
如题:编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入, 使得计算结果总是100的程序,并输出所有的可能性。 例如:1 + 2 + 34–5 + 67–8 + 9 = 100。 import java.util.Stack; public class Solution { static int count = 0; static int countSum = 0; public static void execute(int start, Stack<Integer> stack, int sum, Stack<String> operate, final int N) { if (start == N) { if (sum == 100) { for (int i = 0; i < stack.size(); i++) { System.out.print(stack.get(i)); if (i < operate.size()) { System.out.print(" " + operate.get(i) + " "); } } System.out.println(); count++; } countSum++; return; } int next = start + 1; // j=0代表两数之间相连,j=1代表加,j=2代表减; for (int j = 0; j < 3; j++) { if (j == 0) { int preInt = stack.pop(); int current = preInt * 10 + next; stack.push(current); if (!operate.isEmpty() && operate.peek().equals("+")) { sum = sum - preInt + current; } else if (!operate.isEmpty() && operate.peek().equals("-")) { sum = sum + preInt - current; } else { sum = sum - preInt + current; } execute(next, stack, sum, operate, N); // 现场还原 int reback = stack.pop(); int preBack = reback / 10; stack.push(preBack); if (!operate.isEmpty() && operate.peek().equals("+")) { sum = sum - reback + preBack; } else if (!operate.isEmpty() && operate.peek().equals("-")) { sum = sum + reback - preBack; } else { sum = sum - reback + preBack; } } else if (j == 1) { sum = sum + next; operate.push("+"); stack.push(next); execute(next, stack, sum, operate, N); operate.pop(); int addNum = stack.pop(); sum = sum - addNum; } else if (j == 2) { sum = sum - next; operate.push("-"); stack.push(next); execute(next, stack, sum, operate, N); operate.pop(); int decNum = stack.pop(); sum = sum + decNum; } } } public static void main(String[] args) { Stack<Integer> stack = new Stack<Integer>(); stack.push(1); execute(1, stack, 1, new Stack<String>(), 9); System.out.println(count); System.out.println(countSum); } }
相关文章推荐
- 编写一个在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
- 文章标题编写一个在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的程序
- (scala实现) 在1到9(顺序不能变)之间插入+或-或什么都不插入,使得计算结果总是100
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 输出一个整数的每一位,计算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
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。 输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。 补充说明: 1、操作数为
- 编写一个程序计算100以内的smith数,smith数的概念: 一个非素数,其各位数之和等于其所有质因数的个位数之和。 例如:4=2*2,4=2+2