华为机试-020-中等-HJ20.密码验证合格程序
一、描述
- 密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足 1 ≤ n ≤ 100
1.1、输入描述
- 一组字符串。
1.2、输出描述
- 如果符合要求输出:OK,否则输出NG
二、示例
2.1、示例1
输入:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出:
OK
NG
NG
OK
三、答案(java)
3.1、方法一
package com.tzq.hwod;
import java.util.Scanner;
/**
* @author tzq
* @version 1.0.0
* @ClassName Main.java
* @Description 密码验证合格程序
* @createTime 2022年01月18日 15:47:00
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String password = scanner.nextLine();
char[] chars = password.toCharArray();
// Pattern number = Pattern.compile("[0-9]");
// Pattern minChar = Pattern.compile("[a-z]");
// Pattern bigChar = Pattern.compile("[A-Z]");
// Pattern oChar = Pattern.compile("[^a-zA-Z0-9]");
if (password.length() > 8) {
int a = 0, b = 0, e = 0, d = 0;
boolean err = true;
for (char c : chars) {
// 数字
if (c >= '0' && c <= '9') {
a = 1;
} else if (c >= 'a' && c <= 'z') {
b = 1;
} else if (c >= 'A' && c <= 'Z') {
d = 1;
} else if (c == ' ' || c == '\n') {
// 存在空格或换行
err = false;
break;
} else {
e = 1;
}
}
if (err) {
if ((a + b + d + e) >= 3) {
if (reStr(password)) {
System.out.println("OK");
} else {
// 重复字符串
System.out.println("NG");
}
} else {
// 字符种类小于三种
System.out.println("NG");
}
} else {
// 存在空格
System.out.println("NG");
}
} else {
// 长度小于等于8
System.out.println("NG");
}
}
}
private static boolean reStr(String s) {
for (int i = 3; i < s.length(); i++) {
if (s.substring(i).contains(s.substring(i - 3, i))) {
return false;
}
}
return true;
}
}
四、答案(python 3)
4.1、方法一
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def check(s):
if len(s) <= 8:
return 0
a, b, c, d = 0, 0, 0, 0
for item in s:
if ord('a') <= ord(item) <= ord('z'):
a = 1
elif ord('A') <= ord(item) <= ord('Z'):
b = 1
elif ord('0') <= ord(item) <= ord('9'):
c = 1
else:
d = 1
if a + b + c + d < 3:
return 0
for i in range(len(s)-3):
if len(s.split(s[i:i+3])) >= 3:
return 0
return 1
while 1:
try:
print('OK' if check(input()) else 'NG')
except:
break