zoukankan      html  css  js  c++  java
  • Java实现蓝桥杯七对数字

    今有7对数字:两个1,两个2,两个3,…两个7,把它们排成一行。
    要求,两个1间有1个其它数字,两个2间有2个其它数字,以此类推,两个7之间有7个其它数字。如下就是一个符合要求的排列:

    17126425374635

    当然,如果把它倒过来,也是符合要求的。

    请你找出另一种符合要求的排列法,并且这个排列法是以74开头的。

    注意:只填写这个14位的整数,不能填写任何多余的内容,比如说明注释等。

    答案:74151643752362

    public class Main {
        
        public void swap(int[] A, int i, int j) {
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        }
        
        public void dfs(int[] A, int step) {
            if(step == A.length) {
                StringBuffer s = new StringBuffer("");
                for(int i = 0;i < A.length;i++)
                    s.append(A[i]);
                boolean judge = true;
                String s1 = s.toString();
                for(int i = 1;i <= 7;i++) {
                    int a = s1.indexOf(i+"");
                    int b = s1.lastIndexOf(""+i);
                    if(b - a != i + 1) {
                        judge = false;
                        break;
                    }
                }
                if(judge)
                    System.out.println(s1);
                return;
            } else {
                for(int i = step;i < A.length;i++) {
                    if(A[step] == 7 || A[step] == 4)
                        dfs(A, step + 1);
                    if(A[i] == 7 || A[i] == 4)
                        continue;
                    swap(A, i, step);
                    dfs(A, step + 1);
                    swap(A, i, step);
                }
            }
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            int[] A = {7,4,1,1,2,2,4,3,7,3,5,5,6,6};
            test.dfs(A, 0);
        }
    }
    
  • 相关阅读:
    systemd管理服务
    卷积神经网络
    matplotlib-3.2.1
    pandas-1.0.3
    numpy-1.18.4
    降维
    无监督学习-聚类
    集成学习
    人工神经网络
    贝叶斯分类
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078052.html
Copyright © 2011-2022 走看看