zoukankan      html  css  js  c++  java
  • java实现第六届蓝桥杯三羊献瑞

    三羊献瑞

    题目描述
    观察下面的加法算式:

      祥 瑞 生 辉
    
    • 三 羊 献 瑞

    三 羊 生 瑞 气

    (如果有对齐问题,可以参看【图1.jpg】)

    其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。

    请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。
    在这里插入图片描述

    结果:1085

    import java.util.Arrays;
    
    public class Main {
        
        public static boolean judge(int[] A) {
            int[] tempA = new int[A.length];
            for(int i = 0;i < A.length;i++)
                tempA[i] = A[i];
            Arrays.sort(tempA);   //对tempA元素进行从小到大排序
            for(int i = 1;i < tempA.length;i++) {
                if(tempA[i - 1] == tempA[i])
                    return false;
            }
            return true;
            
        }
        
        public static boolean judge1(int[] A, int[] B) {
            int[] temp = new int[A.length + B.length - 1];
            int i = 0;
            for(;i < A.length;i++)
                temp[i] = A[i];
            for(;i < temp.length;i++)
                temp[i] = B[i - A.length];
            Arrays.sort(temp);
            for(i = 1;i < temp.length;i++) {
                if(temp[i - 1] == temp[i])
                    return false;
            }
            return true;
        }
        
        
        public static void main(String[] args) {
            for(int i = 1000;i <= 9999;i++) {
                int[] A = new int[4];
                A[0] = i / 1000;
                A[1] = i / 100 % 10;
                A[2] = i / 10 % 10;
                A[3] = i % 10;
                if(judge(A) == false)
                    continue;
                for(int j = 1000;j <= 9999;j++) {
                    int[] B = new int[4];
                    B[0] = j / 1000;
                    B[1] = j / 100 % 10;
                    B[2] = j / 10 % 10;
                    B[3] = j % 10;
                    if(judge(B) == false)
                        continue;
                    if(B[3] != A[1])
                        continue;
                    if(judge1(A, B) == false)
                        continue;
                    int temp = i + j;
                    if(temp < 9999 || temp > 99999)
                        continue;
                    int[] C = new int[5];
                    C[0] = temp / 10000;
                    C[1] = temp / 1000 % 10;
                    C[2] = temp / 100 % 10;
                    C[3] = temp / 10 % 10;
                    C[4] = temp % 10;
                    if(C[0] == B[0] && C[1] == B[1] && C[2] == A[2] && C[3] == A[1]) {
                        if(C[4] != A[0] && C[4] != A[1] && C[4] != A[2] && C[4] != A[3]) {
                            if(C[4] != B[0] && C[4] != B[1] && C[4] != B[2] && C[4] != B[3])
                                System.out.println("i = "+i+", j = "+j+", temp = "+temp);
                        }
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    【MM 】采购合同成批维护
    【FICO 汇率】汇率
    【S4 MM】S4中继续使用MB系统事务代码(转)
    【MM 交货成本】Unplanned Delivery Cost
    Tracer Deployment UVALive
    The Best Path HDU
    Artwork Gym
    PTA 银行排队问题之单队列多窗口加VIP服务 队列+模拟
    Counting Cliques HDU
    Do not pour out HDU
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13077367.html
Copyright © 2011-2022 走看看