华为机试20-密码验证合格程序

该博客介绍了华为机试中的一道题目,要求编写程序验证密码是否合格。合格密码需满足:长度超过8位,包含大小写字母、数字及其它符号中的至少三种,并且不能存在相同长度大于2的子串重复。文章给出了输入输出示例,帮助理解题意。

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

描述 密码要求:

1.长度超过8位

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

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

输入描述: 一组或多组长度超过2的字符串。每组占一行

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

示例1
输入:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出:
OK
NG
NG
OK

Solution

#include<iostream>
#include<string>

using namespace std;

bool checkInclude(string str){
    int count = 0;
    for(int i = 0;i<str.size();i++)
        if(str.at(i)>=48&&str.at(i)<=57){
            count++;
            break;
        }
    for(int i = 0;i<str.size();i++)
        if(str.at(i)>=65&&str.at(i)<=90){
            count++;
            break;
        }
    for(int i = 0;i<str.size();i++)
        if(str.at(i)>=97&&str.at(i)<=120){
            count++;
            break;
        }
    for(int i = 0;i<str.size();i++)
        if(str.at(i)<48||str.at(i)>57&&str.at(i)<65||str.at(i)>90&&str.at(i)<97||str.at(i)>120){
            count++;
            break;
        }
    if(count>=3)
        return true;
    else
        return false;
}
bool checkRepeat(string str){
	/*for(int i = 0;i<str.size()-1;i++){
		cout<<str.substr(i,i+2)<<endl;
		cout<<str.substr(i+1)<<endl;
		cout<<str.substr(i+1).find(str.substr(i,i+2))<<endl;
		if(str.substr(i+1).find(str.substr(i,i+2))!=str.npos){
			return false;
		}
		
	}*/
	
	while(str.size()>=2){
		//cout<<str.substr(0,3)<<endl;
		//cout<<str.substr(1)<<endl;
		//cout<<str.substr(1).find(str.substr(0,3))<<endl;
		if(str.substr(1).find(str.substr(0,3))!=str.npos)
			return false;
		str = str.substr(1);
	}
    return true;
};

int main(){
    string str;
    while(cin>>str){
        if(str.size()>8&&checkInclude(str)&&checkRepeat(str))
            cout<<"OK"<<endl;
        else
            cout<<"NG"<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值