牛客網——華為機試(題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;
		}
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM