牛客网——华为机试(题20:密码验证合格程序)(Java)

题目描述:

密码要求:

1.长度超过8位

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

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

说明:长度超过2的子串

输入描述:

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

输出描述:

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

示例1:

输入:

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

输出:

OK
NG
NG
OK

代码: 

import java.util.Scanner;

public class Main {
	public static void main ( String[] args ) {
		Scanner in = new Scanner( System.in );
		while ( in.hasNextLine() ) {
			String s = in.nextLine();
			if ( check_length( s )) {
				if ( check_kinds( s ) ) {
					if ( check_repeat( s ) ) {
						System.out.println("OK");
					}
					else {
						System.out.println("NG");
					}
				}
				else {
					System.out.println("NG");
				}
			}
			else {
				System.out.println("NG");
			}
		}
		in.close();
	}
	public static boolean check_length ( String str ) {
		if ( str.length() <= 8 ) {
			return false;
		}
		else {
			return true;
		}
	}
	public static boolean check_kinds ( String str ) {
		int num = 0;
		int lowerletters = 0;
		int upperletters = 0;
		int elseletters = 0;
		for ( int i = 0 ; i < str.length() ; i++ ) {
			if ( Character.isDigit(str.charAt(i)) ) {
				num++;
				continue;
			}
			if ( Character.isLowerCase(str.charAt(i)) ) {
				lowerletters++;
				continue;
			}
			if ( Character.isUpperCase(str.charAt(i)) ) {
				upperletters++;
				continue;
			}
			else {
			    elseletters++;
			}
		}
		if ( ( num != 0 && lowerletters != 0 && upperletters != 0 ) || ( elseletters != 0 && lowerletters != 0 && upperletters != 0 )
				|| ( num != 0 && elseletters != 0 && upperletters != 0 ) || (num != 0 && lowerletters != 0 && elseletters != 0 )) {
			return true;
		}
		else {
			return false;
		}
	}
	public static boolean check_repeat ( String str ) {
		int num = 0;
		for ( int i = 3 ; i <= str.length()/2 ; i++ ) {
			for ( int j = 0 ; j < str.length() - i ; j++ ) {
				String s = str.substring(j, j + i );
				if ( str.indexOf(s) != str.lastIndexOf(s) ) {
					num++;
				}
			}
		}
		if ( num == 0) {
			return true;
		}
		else {
			return false;
		}
	}
}

你可能感兴趣的:(牛客网华为机试Java)