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);
    }
    
  • 相关阅读:
    Scintilla开源库使用指南
    HLSL中的MUL指令深层剖析
    用游戏编辑器制作MOD脱颖而出
    乘法快速算法
    Python3之format
    Python3之Zip
    python时间处理之datetime
    Python3 每次处理一个字符
    SQL 循环插入10000条
    一个.py引用另一个.py中的方法
  • 原文地址:https://www.cnblogs.com/InternetJava/p/12543218.html
Copyright © 2011-2022 走看看