看下面的算式:
□□ x □□ = □□ x □□□
它表示:两个两位数相乘等于一个两位数乘以一个三位数。
如果没有限定条件,这样的例子很多。
但目前的限定是:这9个方块,表示1~9的9个数字,不包含0。
该算式中1至9的每个数字出现且只出现一次!
比如:
46 x 79 = 23 x 158
54 x 69 = 27 x 138
54 x 93 = 27 x 186
.....
请编程,输出所有可能的情况!
import java.util.ArrayList; import java.util.List; public class ChessNum { static List<Integer> list; static int one,two,three,four; static int a,b,c,d,e,f,g,h,z; public static void main(String[] args) { list = new ArrayList<Integer>(); getList(); for (int i = 1; i <= 9; i++) { a = list.get(i); list.set(i, 0); for (int j = 1; j <= 9; j++) { if (list.get(j) != 0) { b = list.get(j); list.set(j, 0); for (int k = 1; k <= 9; k++) { if (list.get(k) != 0) { c = list.get(k); list.set(k, 0); for (int l = 1; l <= 9; l++) { if (list.get(l) != 0) { d = list.get(l); list.set(l, 0); for (int m = 1; m <= 9; m++) { if (list.get(m) != 0) { e = list.get(m); list.set(m, 0); for (int n = 1; n <= 9; n++) { if (list.get(n) != 0) { f = list.get(n); list.set(n, 0); for (int o = 1; o <= 9; o++) { if (list.get(o) != 0) { g = list.get(o); list.set(o, 0); for (int p = 1; p <= 9; p++) { if (list.get(p) != 0) { h = list.get(p); list.set(p, 0); for (int q = 1; q <= 9; q++) { if (list.get(q) != 0) { z = list.get(q); list.set( q, 0); if(builtOne(a, b)*builtTwo(c,d)==builtThree(e, f)*builtFour(g, h, z)) { System.out.println(builtOne(a, b)+"x"+builtTwo(c,d)+"="+builtThree(e, f)+"x"+builtFour(g, h, z)); } } } addList(list); list.set(i,0); list.set(j,0); list.set(k,0); list.set(l,0); list.set(m,0); list.set(n,0); list.set(0,0); } } addList(list); list.set(i,0); list.set(j,0); list.set(k,0); list.set(l,0); list.set(m,0); list.set(n,0); } } addList(list); list.set(i,0); list.set(j,0); list.set(k,0); list.set(l,0); list.set(m,0); } } addList(list); list.set(i,0); list.set(j,0); list.set(k,0); list.set(l,0); } } addList(list); list.set(i,0); list.set(j,0); list.set(k,0); } } addList(list); list.set(i,0); list.set(j,0); } } addList(list); list.set(i,0); } } addList(list); } } public static void addList(List<Integer> list) { for (int r = 1; r <= 9; r++) { list.set(r, r); } } private static void getList() { for(int i=0;i<=9;i++) { list.add(i,i); } } public static int builtOne(int a,int b) { return a*10+b; } public static int builtTwo(int a,int b) { return a*10+b; } public static int builtThree(int a,int b) { return a*10+b; } public static int builtFour(int a,int b,int c) { return a*100+b*10+c; } }
结果:存在重复,这个暴力运算实在是让人头晕
46x79=23x158
54x69=27x138
54x93=27x186
56x73=14x292
58x67=29x134
58x69=23x174
58x73=29x146
58x96=32x174
63x74=18x259
64x79=32x158
67x58=29x134
69x54=27x138
69x58=23x174
73x56=14x292
73x58=29x146
73x96=12x584
74x63=18x259
76x98=14x532
79x46=23x158
79x64=32x158
79x84=12x553
84x79=12x553
93x54=27x186
96x58=32x174
96x73=12x584
98x76=14x532