凑算式
B DEF
A + --- + ------- = 10
C GHI
这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?
注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。
(碰到除法问题,要特别注意,如题目未事先声明进行整除,均首先消除分母,进行乘法运算,这样可以消除浮点数误差)
public class Main{
public static int count = 0;
public static boolean check(int[] A) {
int a1 = A[0]*A[2]*(A[6]*100+A[7]*10+A[8]);
int a2 = A[1]*(A[6]*100+A[7]*10+A[8]);
int a3 = (A[3]*100+A[4]*10+A[5])*A[2];
if (a1+a2+a3 == 10*A[2]*(A[6]*100+A[7]*10+A[8])) {
return true;
}
return false;
}
public static void swap(int[] A,int a,int b) {
int temp = A[a];
A[a] = A[b];
A[b] = temp;
}
private static void dfs(int[] A, int step) {
if (step == A.length) {
if (check(A)) {
count++;
}return;
}else {
for (int i = step; i < A.length; i++) {
swap(A,i,step);
dfs(A, step+1);
swap(A,i,step);
}
}
return;
}
public static void main(String[] args) {
int[] A = {1,2,3,4,5,6,7,8,9};
dfs(A,0);
System.out.println(count);
}
}