zerosum

来源:互联网 发布:淘宝买护肤品靠谱吗 编辑:程序博客网 时间:2024/06/05 21:50
 
/*ID: daniel.20LANG: JAVATASK: zerosum */import java.io.*;import java.util.*;class pair {    int currentNum;    int value;    String sequence;    public pair(int a, int b, String c) {        currentNum = a;//record the num we have processed, for example, currentNum==3, then 4 is the next num wait to process        value = b;//the value of the expression        sequence = c;//record the expression    }}public class zerosum {    static int n = 9;    static void initial() throws Exception {        BufferedReader f = new BufferedReader(new FileReader("zerosum.in"));        n = Integer.parseInt(f.readLine());    }    static int findLastMark(String k) {  //start from the last position and keep going backwards until I find the first non-digit char        for (int i = k.length() - 1; i >= 0; --i) {            if (k.charAt(i) == '+' || k.charAt(i) == '-') {                return i;            }        }        return 0;    }    static String removeSpace(String a) {        String tmp = "";        for (int i = 0; i < a.length(); ++i) {            if (a.charAt(i) != ' ') {                tmp += a.charAt(i);            }        }        return tmp;    }    static void BFS() throws Exception {        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("zerosum.out")));        char[] mark = {' ', '+', '-'}; //possible marks in alaphbatic order        Queue<pair> q = new LinkedList<pair>();        q.add(new pair(1, 1, "1"));        while (!q.isEmpty()) {            pair t = q.poll();            int nextNum = t.currentNum + 1;            if (nextNum > n) {//terminate condition                break;            }            for (int j = 0; j < mark.length; ++j) {                pair temp;                if (j == 1) {//if the mark is "+"                    temp = new pair(nextNum, t.value + nextNum, t.sequence + "+" + nextNum);                } else if (j == 2) {//if the mark is "-"                    temp = new pair(nextNum, t.value - nextNum, t.sequence + "-" + nextNum);                } else {                    int pos = findLastMark(t.sequence);//find last mark                    int num1, num2;                    if (pos != 0) {                        num1 = Integer.parseInt(removeSpace(t.sequence.substring(pos + 1)));//find last number                        num2 = num1 * 10 + nextNum;//combine two or more numbers                    } else {                        num1 = Integer.parseInt(removeSpace(t.sequence));                        num2 = num1 * 10 + nextNum;                    }                    if (t.sequence.charAt(pos) == '+') {                        temp = new pair(nextNum, t.value - num1 + num2, t.sequence.substring(0, pos + 1) + num1 + " " + nextNum);                    } else if (t.sequence.charAt(pos) == '-') {                        temp = new pair(nextNum, t.value + num1 - num2, t.sequence.substring(0, pos + 1) + num1 + " " + nextNum);                    } else {                        temp = new pair(nextNum, num2, num1 + " " + nextNum);                    }                }                if (temp.currentNum == n && temp.value == 0) {                    out.println(temp.sequence);                }                q.add(temp);//put into queue            }        }        out.close();    }    public static void main(String[] args) throws Exception {        initial();        BFS();        System.exit(0);    }}


题目不难,由于数据不大,暴力枚举就行...

代码写得不好,我都不好意思说了,自己都不想看,真尼玛水,学了这么就java,一个小时就写出这样的东西,我都不能忍自己了

哎,上次说的那个背包也没重写过,真操蛋。

再等几天吧,这个chapter完了估计我心情会好!!!

然后再写一遍^_^

原创粉丝点击