编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。

import java.util.ArrayList;
import java.util.List;
 
 
public class TryTest {

public static List<String> list = new ArrayList<String>();
public static int num = 8 ;
/**
     * 构造字符串的所有排序组合
     * @param str 将要组合成的字符
     * @param nstr 源字符串集
     */

public static void group(String str, String nstr) {
String temp = "";
addStr(temp, "+");
addStr(temp, "-");
addStr(temp, "o");

}
 
public static void addStr(String str, String nstr) {

if (num != str.length()) {


String temp = "";
temp = str + nstr;
if (temp.length() == num){
list.add(temp);
}
System.out.println(temp);
addStr(temp, "+");
addStr(temp, "-");
addStr(temp, "o");
}
}

public static void main(String[] args) {
TryTest.group("", "+-o");
System.out.println(list.size());
}

}

 


import java.util.ArrayList;  
import java.util.List;  
import java.util.EmptyStackException;  
  
public class MyStack<E extends Object> {  
    private List<E> pool = new ArrayList<E>();  
  
    public MyStack() {  
    }  
  
    public void clear() {  
        pool.clear();  
    }  
  
    public boolean isEmpty() {  
        return pool.isEmpty();  
    }  
  
    /** 
     * 获取栈顶元素 
     * */  
    public E getTopObjcet() {  
        if (isEmpty()) {return null;}  
        return pool.get(0);  
    }  
  
    /** 
     * 弹出栈操作 
     * */  
    public E pop() {  
        if (isEmpty()) {throw new EmptyStackException();}  
        return pool.remove(pool.size() - 1);  
    }  
  
    /** 
     * 压入栈 
     * */  
    public void push(E e) {  
        //if (isEmpty()) {throw new EmptyStackException();}  
        pool.add(e);  
    }  
  
    /** 
     * 获取当前栈大小 
     * */  
    public int getStatckSize() {  
        //if (isEmpty()) {throw new EmptyStackException();}  
        return pool.size();  
    }  
  
}  


import java.util.ArrayList;
import java.util.List;
 
public class Question {

private static List<Integer> sumList = new ArrayList<Integer>() ;

public static void main(String[] args) {
 
TryTest.group("", "+-o");
System.out.println(TryTest.list.size());
new Question().init();

System.out.println(sumList.size());
int max = new Question().sort();
System.out.println("max:"+max);

int min = new Question().getMin();
System.out.println("min:"+min);
}


public void init() {

this.operate();

}

public void operate() {

MyStack<Integer> intStack = new MyStack<Integer>();
MyStack<String> strStack = new MyStack<String>();
for (int i = 0 ; i < TryTest.list.size(); i++) {

intStack.clear();
strStack.clear();
for (int t = 9 ; t > 0 ; t--) {
intStack.push(t);
}

for (int j = TryTest.list.get(i).length() ; j > 0 ; j--) {
strStack.push(TryTest.list.get(i).substring(j-1,j)); 

}  
this.calculate( intStack , strStack);
}
}

public void calculate(MyStack<Integer> intStack, MyStack<String> strStack) {
int size = intStack.getStatckSize();
int strStackSize = strStack.getStatckSize();
 
/*for (int i = 0; i < size; i++) {
System.out.println(intStack.pop());
}*/
int temp = intStack.pop();
for (int i = 0; i < strStackSize; i++) {
 
switch (strStack.pop()) {
case "+":
temp+=intStack.pop();
 
break;
case "-":
temp-=intStack.pop();
 
break;
case "o":
temp=Integer.parseInt(temp+""+intStack.pop()); 
break;

default:  
break;
}

}
System.out.println(temp);
sumList.add(temp);
}

public int sort(){

int max = sumList.get(0) ;
for (int i = 0; i < sumList.size()-1; i++) {
if(max < sumList.get(i+1)){
max = sumList.get(i+1);
}
}
return max;
}

public int getMin(){

int min = sumList.get(0) ;
for (int i = 0; i < sumList.size()-1; i++) {
if(min > sumList.get(i+1)){
min = sumList.get(i+1);
}
}
return min;
}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值