JAVA-------------华为上机-------------密码验证合格程序

题目描述

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有相同长度超2的子串重复

说明:长度超过2的子串

输入描述:

一组或多组长度超过2的子符串。每组占一行

输出描述:

如果符合要求输出:OK,否则输出NG

输入

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

输出

OK
NG
NG
OK

 

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws Exception {
        while (scanner.hasNextLine()) {
            String password = scanner.nextLine();
            if (confirmPassword(password)) {
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }

        }


    }

    private static boolean confirmPassword(String password) {
        if (password.length() <= 8 || subStringRepeat(password)) {
            return false;
        }
        if(!containSomething(password)){
            return  false;
        }
        return true;
    }

    private static boolean subStringRepeat(String password) {
        //双重for 外层for循环代表字串长度,从3-->n-1
        //内层for循环代表在该长度下所有字串。
        for (int i = 3; i < password.length(); i++) {
            for (int j = 0; j < password.length() - i; j++) {
                String subString = password.substring(j, j + i);
                if (password.indexOf(subString) != password.lastIndexOf(subString)) {//如果不相等,说明字串重复
                    return true;
                }
            }
        }
        return false;
    }

    private static boolean containSomething(String password) {
        String xiaoXie = "qwertyuiopasdfghjklzxcvbnm";
        String daXie = "QWERTYUIOPASDFGHJKLZXCVBNM";
        String num = "1234567890";
        int f1 = 0, f2 = 0, f3 = 0, f4 = 0;
        for (int i = 0; i < password.length(); i++) {
            String c = password.charAt(i) + "";
            if (xiaoXie.contains(c)) {
                f1 = 1;
            } else if (daXie.contains(c)) {
                f2 = 1;
            } else if (num.contains(c)) {
                f3 = 1;
            } else {
                f4 = 1;
            }
        }
        return f1 + f2 + f3 + f4 >= 3;
    }
}

 

 

 

 

posted @ 2018-05-21 15:13  过道  阅读(384)  评论(0编辑  收藏  举报