华为机试-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

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tzq@2018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值