天天看點

編寫一個在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 ;

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();

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;

}

}

繼續閱讀