package 第10周_蓝桥杯;
public class 凑算式 {
/*
* 这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。 比如: 6+8/3+952/714 就是一种解法, 5+3/1+972/486
* 是另一种解法。
*
* 这个算式一共有多少种解法?——29
*/
static int total = 0;
public static void main(String[] args) {
float[] num = new float[9];
for (int i = 1; i < 10; i++)
num[i - 1] = i;
handle(num, 0);
System.out.println(total);
}
private static void handle(float[] num, int now) {
if (now == 8) {
if (check(num)) {
total++;
for (int i = 0; i < 9; i++)
System.out.print(num[i]);
System.out.println();
}
return;
}
//全排列的方法
for (int i = now; i < 9; i++) {
float temp = num[now];num[now] = num[i];num[i] = temp;
handle(num, now + 1);
temp = num[now];num[now] = num[i];num[i] = temp;
}
}
private static boolean check(float[] num) {
float a = num[0];
float b = num[1];
float c = num[2];
float d = num[3] * 100 + num[4] * 10 + num[5];// DEF
float e = num[6] * 100 + num[7] * 10 + num[8];// GHI
if (a * c * e + b * e + d * c == 10 * c * e) {
return true;
}
return false;
}
}