日期:2014-05-20 浏览次数:20621 次
package com.esen.chenf.test; public class Test8 { public static void main(String[] args) { int[][] a = new int[10][2]; int num1 = 0, num2 = 0, num3 = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { for (int k = 0; k < 9; k++) { init(a); if (a[i][1] == 0 && a[j][1] == 0 && a[k][1] == 0) { num1 = a[i][0] * 100 + a[j][0] * 10 + a[k][0]; a[i][1] = 1; a[j][1] = 1; a[k][1] = 1; } num2 = num1 * 2; if(!avaliable(num2, a)){ continue; } num3 = num1*3; if(!avaliable(num3, a)){ continue; } System.out.println(num1); System.out.println(num2); System.out.println(num3); } } } } private static void init(int[][] a) { for (int i = 0; i < 9; i++) { a[i][0] = i + 1; a[i][1] = 0; } } public static boolean avaliable(int num, int[][] a) { if (num < 100 || num >= 1000) return false; for (int i = 0; i < 3; i++) { int k = num % 10 - 1; if(k == -1) return false; if (a[k][1] == 1) return false; a[k][1] = 1; num /= 10; } return true; } }
------解决方案--------------------
for example
int[] sum = new int[3]; int[][] matrix = new int[3][3]; List<Integer> list = new ArrayList<Integer>(); for (int i=123, cnt=1; i<333; i++) { sum[0] = i; sum[1] = i*2; sum[2] = i*3; list.clear(); for (int j=0, k=100, v=0; j<sum.length; j++, k=100) { while (k > 0) { v = sum[j]/k; sum[j] %= k; k /= 10; if (! list.contains(v) && v != 0) {list.add(v);} } } if (list.size() == 9) { System.out.printf("matrix-%d\n", cnt++); for (int j=0; j<list.size(); j++) { matrix[j/3][j%3] = list.get(j); System.out.printf("%d", list.get(j)); if ((j+1)%3 == 0) {System.out.println();} } } }
------解决方案--------------------
别把问题想复杂了
public static void main(String[] args) { int m = 0; int n = 0; String str = null; for(int i=123;i<345;i++){ m = i*2; n = i*3; str = "" + i + m + n; byte[] b = str.getBytes(); Arrays.sort(b); str = new String(b); if("123456789".equals(str)){ System.out.println(i + ";" + m + ";" + n); } } }