密码验证合格程序/华为机试(C/C++)

本文介绍了一种用于验证密码强度的算法实现,确保密码满足特定的安全要求:长度超过8位、包含不同类型字符至少三种且无重复子串。提供了两种不同的C++实现方案,并通过示例展示了如何使用这些算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

密码要求:

1.长度超过8位

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

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

说明:长度超过2的子串

 

输入描述:

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

输出描述:

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

示例1

输入

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

输出

OK
NG
NG
OK

代码1:

//第二十题 密码验证合格程序
#include<iostream>
#include<vector>
#include<string>
#include<string.h>
using namespace std;
bool EncodePassword(string test)
{
	int i_flag = 0;
	size_t i_max = test.length();
	if (i_max < 9)//condition 1
	{
		return false;
	}
	for (int i = 0; i < i_max; i++)
	{
		if (isdigit(test.at(i)))
		{
			i_flag = i_flag | 1;
		}
		else if (isupper(test.at(i)))
		{
			i_flag = i_flag | 2;
		}
		else if (islower(test.at(i)))
		{
			i_flag = i_flag | 4;
		}
		else
		{
			i_flag = i_flag | 8;
		}
	}
	if (i_flag != 7 && i_flag != 11 && i_flag < 13)//condition 2
	{
		return false;
	}
	int i_max2 = i_max - 2;
	for (int i = 0; i < i_max2; i++)//condition 3
	{
		for (int j = i + 1; j < i_max2; j++)
		{
			if (test[i] == test[j] && test[i + 1] == test[j + 1] && test[i+2]== test[j+2])
			{
				return false;
			}
		}
	}
	return true;
}
int main()
{
	vector<string>v_instrs;
	string in_str;
	while (cin>>in_str)
	{
		v_instrs.push_back(in_str);
	}
	size_t i_max = v_instrs.size();
	for (int i = 0; i < i_max; i++)
	{
		if (EncodePassword(v_instrs.at(i)))
		{
			cout << "OK" << endl;
		}
		else
		{
			cout << "NG" << endl;
		}
	}
    system("pause");
	return 0;
}

在线测试,4ms

 

代码2:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str;
	while (cin >> str) 
	{
		int flag[4] = { 0 };
		if (str.size() <= 8) 
			goto NG;
		for (int i = 0; i < str.size(); ++i)
			if (str[i] >= 'a' && str[i] <= 'z') 
				flag[0] = 1;
			else if (str[i] >= 'A' && str[i] <= 'Z') 
				flag[1] = 1;
			else if (str[i] >= '0' && str[i] <= '9') 
				flag[2] = 1;
			else 
				flag[3] = 1;
			if (flag[0] + flag[1] + flag[2] + flag[3] < 3)
				goto NG;
			for (int i = 0; i <= str.size() - 6; i++)
				for (int j = i + 3; j < str.size(); j++)
					if (str[i] == str[j] && str[i + 1] == str[j + 1] && str[i + 2] == str[j + 2]) 
						goto NG;
		OK:
			cout << "OK" << endl; continue;
		NG:
			cout << "NG" << endl;

	}
	system("pause");
	return 0;
}

代码2不是本人写的。挺厉害的,很跳,哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值