锁定老帖子 主题:写个算法
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-03-29
这个可以用图的遍历做,用二维数组存图的邻接矩阵。将矩阵的row>col的元素填充1,其它为0,控制出现1+9 和 9+1重复的情况。
import java.util.ArrayList; import java.util.List; public class Test { public static int n = 10; public static int[] source = new int[n - 1]; //存储1到9的数字 public static boolean[] visited = new boolean[n - 1]; //访问标记数组 public static int metrix[][] = new int[n - 1][n - 1]; //邻接矩阵 public static List<Integer> result = new ArrayList<Integer>(); public static void main(String[] args) { for (int i = 0; i < n - 1; i++) { source[i] = i + 1; // 填充数字1....9 for (int j = 0; j < n - 1; j++) { if (i > j) metrix[i][j] = 1; // 填充连接矩阵下半边 } } for (int i = 0; i < n - 1; i++) { depthFirstSearch(i); //从source[0]开始图的深度优先遍历 } } public static void depthFirstSearch(int index) { visited[index] = true; result.add(source[index]); if (getSum() == n) { displaySolution(); } else if (getSum() > n) { result.remove(result.size() - 1); visited[index] = false; return; } else if (getSum() < n) { for (int i = 0; i < n - 1; i++) { if (metrix[index][i] == 1 && visited[i] == false) { depthFirstSearch(i); } else continue; } } result.remove(result.size() - 1); visited[index] = false; } public static int getSum() { int sum = 0; for (Integer o : result) { sum += o; } return sum; } public static void displaySolution() { for (Integer o : result) System.out.print(o + " "); System.out.println(); } } |
|
返回顶楼 | |
发表时间:2011-03-31
算法设计上不是有个例子么 完全一样
|
|
返回顶楼 | |
发表时间:2011-03-31
递归应该是可以解决的吧,下面的代码测试ok ,仅供参考~
public class JTest { public static void dpxy(int x, int y, String result){ if(result == "") result = x + " ="; if(x < y || x <= 0) return; if(y == 1 || x == y){ if(y == 1){ for(int i=0;i<x;i++) if(i == x-1) result += "1"; else result += "1+"; }else{ result += x; } System.out.println(result); return; } result += y + "+"; for(int i=0;i<y;i++){ dpxy(x - y, y-i, result); } } /** * @param args */ public static void main(String[] args) { for(int i=0;i<20;i++) //n=20 dpxy(20, 20-i, ""); } } |
|
返回顶楼 | |
发表时间:2011-04-06
最后修改:2011-04-07
较高效的计算算法
public class IntegerSumArrange { private int param; private int[] array; public void init(int param) { this.param = param; array = new int[param - 1]; for (int i = 0; i < array.length; i++) { array[i] = i + 1; } } public void exhaustion() { Stack<Integer> stack = new Stack<Integer>(); for (int i = 0; i < array.length; i++) { stack.push(array[i]); exhaustion(stack, array, i); stack.pop(); } } public void exhaustion(Stack<Integer> stack, int[] array, int index) { for (int i = index + 1; i < array.length; i++) { int value = calc(stack, array[i]); if (value == param) { print(stack, array[i]); } else if (value < param) { stack.push(array[i]); exhaustion(stack, array, i); stack.pop(); } } } public void print(Stack<Integer> stack, int value) { for (Iterator<Integer> it = stack.iterator(); it.hasNext();) { System.out.print(it.next()); System.out.print("+"); } System.out.print(value); System.out.print("="); System.out.println(param); } public int calc(Stack<Integer> stack, int value) { for (Iterator<Integer> it = stack.iterator(); it.hasNext();) { value += it.next(); } return value; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("please input an integer : "); Integer number = Integer.parseInt(br.readLine()); IntegerSumArrange isa = new IntegerSumArrange(); isa.init(number); isa.exhaustion(); } } |
|
返回顶楼 | |
发表时间:2011-04-08
import java.util.*;
public class MyRecursion{ public void test(List list,int prefix,String strPrefix,int m){ if(prefix == 10) System.out.println(strPrefix.trim()); for(int i=m;i<list.size()&& prefix <= 10;i++){ List<Integer> temp = new ArrayList<Integer>(list); int a = temp.remove(i); test(temp,prefix + a ,strPrefix+" "+a,i); } } public static void main(String[] args){ MyRecursion mr = new MyRecursion(); List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); list.add(8); list.add(9); list.add(10); //list.add(11); mr.test(list,0,"",0); } }code] |
|
返回顶楼 | |
发表时间:2011-10-13
import java.util.Collections; import java.util.LinkedList; public class test { static LinkedList<Integer> list = new LinkedList<Integer>(); static void getSum(int sum, int n) { if (n <= 0 || sum <= 0) return; if (sum < n) { getSum(sum, sum); return; } list.push(n); if (sum == n) { Collections.reverse(list); System.out.println(list.toString()); Collections.reverse(list); } getSum(sum - n, n - 1); list.pop(); getSum(sum, n - 1); } |
|
返回顶楼 | |
发表时间:2011-10-14
乌鸦* 写道 想了很久,没做出来,应该是用递归把,继续研究。
递归的效率很低,1s钟处理100已经差不多到极限了,用动态规划吧。 |
|
返回顶楼 | |
发表时间:2011-10-25
收益匪浅呀
|
|
返回顶楼 | |
发表时间:2012-02-07
其实就是中兴的那道面试题,在中兴的这道题中,令n=m即为lz所求。
package a.test; import java.util.Stack; public class ZhongXing { /** * 输入两个整数 n 和 m ,从数列 1 , 2 , 3.......n 中随意取几个数 , 使其和等于 m , 要求将其中所有的可能组合列出来 */ public static void main(String[] args) { Stack<Integer> stack=new Stack<Integer>(); find(10,10,stack); } public static void find(int sum,int n,Stack<Integer> stack){ if(sum<=0||n<=0)return; if(sum==n){ System.out.print(n+" "); printIntStackNoPop(stack); System.out.println(); } stack.push(n); find(sum-n,n-1,stack); stack.pop(); find(sum,n-1,stack); } public static void printIntStackNoPop(Stack<Integer> stack){ for(int each:stack){ System.out.print(each+ " "); } } } |
|
返回顶楼 | |