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完了估计我心情会好!!!
然后再写一遍^_^
- zerosum
- usaco 2.3 zerosum
- USACO 2.3 Zero Sum (zerosum)
- USACO zerosum DFS 1A
- USACO2.3.3 Zero Sum (zerosum)
- USACO2.3.3 Zero Sum (zerosum)
- usaco 2.3 zerosum 2008.6.21
- 享受递归的潇洒 USACO zerosum
- usaco2.3 zerosum 表达式求值 栈
- SRVPGM BNDDIR建立
- Unit Test
- 那些在Youtube上呼风唤雨,收入数十万美元的大神们
- newintent的教训
- CALL CALLB CALLP调用(数据移植)
- zerosum
- 应用算法的实际情况——简单就是美
- Xcode 中的GDB的命令。
- Commit、Rollback
- 利用栈实现的后缀形式的算术表达式的求值的c++程序
- 转,展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告
- OVRDBF命令
- poj 3694(求边双连通分量 缩点 暴力LCA)
- jQuery获取Select选中的Text和Value(jquery学习备忘录)