Java凑算式

【题目】

     B      DEF
A + --- + ------- = 10
     C      GHI

这个算式中A-I代表1~9的数字,不同的字母代表不同的数字
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?

【分析】

只需要列出九个循环,都从1-9开始,每次循环让每个数各不相同就可以了
然后最后一个循环添加组成的式子是否等于10条件的条件
注意:循环的变量类型不能用int,因为最后需要做除法运算

【代码】

public class Main {
    public static void main(String[] args) {
        int sum = 0; // 求符合条件的数量
        for (double a = 1; a < 10; a++) {
            for (double b = 1; b < 10; b++) {
                if (a != b) {
                    for (double c = 1; c < 10; c++) {
                        if (a != c && b != c) {
                            for (double d = 1; d < 10; d++) {
                                if (a != d && b != d && c != d) {
                                    for (double e = 1; e < 10; e++) {
                                        if (a != e && b != e && c != e && d != e) {
                                            for (double f = 1; f < 10; f++) {
                                                if (a != f && b != f && c != f && d != f && e != f) {
                                                    for (double g = 1; g < 10; g++) {
                                                        if (a != g && b != g && c != g && d != g && e != g && f != g) {
                                                            for (double h = 1; h < 10; h++) {
                                                                if (a != h && b != h && c != h && d != h && e != h && f != h && g != h) {
                                                                    for (double i = 1; i < 10; i++) {
                                                                        if (a != i && b != i && c != i && d != i && e != i && f != i && g != i && h != i) {
                                                                            double value = (d * 100 + e * 10 + f) / (g * 100 + h * 10 + i);
                                                                            if (a + (b / c) + value == 10) {
                                                                                sum++;
                                                                                System.out.println(""+a+"+"+b+"/"+c+"+"+d+e+f+"/"+g+h+i);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println(sum);
    }
}

【答案】 :29

猜你喜欢

转载自blog.csdn.net/our1624204500/article/details/106771099