zoukankan      html  css  js  c++  java
  • java实现填写算式

    ** 填写算式**

    看这个算式:
    ☆☆☆ + ☆☆☆ = ☆☆☆
    如果每个五角星代表 1 ~ 9 的不同的数字。
    这个算式有多少种可能的正确填写方法?
    173 + 286 = 459
    295 + 173 = 468
    173 + 295 = 468
    183 + 492 = 675
    以上都是正确的填写法!
    注意:
    111 + 222 = 333 是错误的填写法!
    因为每个数字必须是不同的!
    也就是说:1~9中的所有数字,每个必须出现且仅出现一次!
    注意:
    不包括数字“0”!
    注意:
    满足加法交换率的式子算两种不同的答案。
    所以答案肯定是个偶数!

    注意:
    只要求计算不同的填法的数目
    不要求列出所有填写法
    更不要求填写源代码!

    答案不要写在这里,请写在“解答.txt”中!

    参考答案:
    336

    public class Main1 {
        
        public static int count = 0;
        
        public void swap(int[] A, int a, int b) {
            int temp = A[a];
            A[a] = A[b];
            A[b] = temp;
        }
        
        public void dfs(int[] A, int step) {
            if(step == A.length) {
                check(A);
                return;
            } else {
                for(int i = step;i < A.length;i++) {
                    swap(A, i, step);
                    dfs(A, step + 1);
                    swap(A, i, step);
                }
            }
            return;
        }
        
        public void check(int[] A) {
            String tempA = "";
            for(int i = 0;i < A.length;i++)
                tempA += A[i];
            int a = Integer.valueOf(tempA.substring(0, 3));
            int b = Integer.valueOf(tempA.substring(3, 6));
            int c = Integer.valueOf(tempA.substring(6, 9));
            if(a + b == c)
                count++;
            return;
        }
        
        public static void main(String[] args) {
            Main1 test = new Main1();
            int[] A = {1,2,3,4,5,6,7,8,9};
            test.dfs(A, 0);
            System.out.println(count);
        }
    }
    
  • 相关阅读:
    Visual Studio开启SSL的支持
    Linux下ls命令使用详解(转)
    Linux下which命令使用详解(转)
    SQLServer出现不允许保存更改的问题解决
    Mac下包管理平台homebrew的使用
    Jenkins使用FTP进行一键部署及回滚2(Windows)(项目实践)
    TGI
    房地产 专题
    Anaconda
    推荐《用Python进行自然语言处理》中文翻译-NLTK配套书
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947316.html
Copyright © 2011-2022 走看看