zoukankan      html  css  js  c++  java
  • 1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、212345等. 要求:”4”不能在第三位,”3”与”5”不能相连。

    private static String[] mustExistNumber = new String[] { “1”, “2”, “2”, “3”, “4”, “5” };

    private static boolean isValidNumber(String str) {
        // 检查是否包含12345这五个数,不包含返回false
        for (String number : mustExistNumber) {
            if (str.indexOf(number) < 0)
                return false;
        }
        // 检查是否有两个2,只有一个返回false
        if (str.lastIndexOf("2") == str.indexOf("2")) {
            return false;
        }
        // 检查4在不在第三位,是返回false
        if (str.charAt(2) == '4') {
            return false;
        }
        // 检查是否存在35在一起,有返回false
        if (str.indexOf("35") >= 0 || str.indexOf("53") >= 0) {
            return false;
        }
        return true;
    }
    
    public static void main(String[] args) {
        int count=0;
        for (int i = 122345; i <=543221; i++) {
            if (isValidNumber(String.valueOf(i))) {
                System.out.println(i);
                count++;
            }
        }
        System.out.println(count);
    }
    
  • 相关阅读:
    Stream中的map
    项目中的process.bpmn的读-过程
    windows10打开switchHost,提示无修改权限
    Windows10安装node.js
    工作中的小发现
    启动redis
    call apply bin 的区别
    利用promise 让 函数按序执行
    uni-app 小程序
    插件 Generate css tree
  • 原文地址:https://www.cnblogs.com/InternetJava/p/12543218.html
Copyright © 2011-2022 走看看