题解 | 密码验证合格程序

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

#include <iostream>
#include<string>
#include<algorithm>
using namespace std;

int main() {
    string str;
    int sublen=0;
    while(getline(cin,str)){
        bool flag=0;
        int comp[4]={0,0,0,0};
        int len =str.size();    //字符长度
        if(len<8) {  //长度超过8
            cout << "NG" <<endl;
            flag=1;
            continue;
        }
        for(int i=0;i<len;i++){
            if(str[i] >='A' && str[i] <= 'Z') {
                comp[0] =1;
                continue;
            }
            if(str[i] >='a' && str[i] <= 'z') {
                comp[1] =1;
                continue;
            }
            if(str[i] >='0' && str[i] <= '9') {
                comp[2] =1;
                continue;
            }
            if(str[i] >='!' && str[i] <= '/') {
                comp[3] =1;
                continue;
            }else if(str[i] >=':' && str[i] <= '@') {
                comp[3] =1;
                continue;
            }else if(str[i] >='[' && str[i] <= '`') {
                comp[3] =1;
                continue;
            }else if(str[i] >='{' && str[i] <= '~') {
                comp[3] =1;
                continue;
            }
        }
        //至少三种字符
        int n = sizeof(comp) / sizeof(comp[0]); // 计算数组长度
        int target = 1;
        int count = std::count(comp, comp + n, target); // 统计元素出现次数
        if(count <3) {  
            cout <<"NG" <<endl;
            flag=1;
            continue;
        }
        //不重复的连续字串
        bool flag2 =0;
        for(int i=0;i<len-5;i++){
            for(int j=i+1;j<len-2;j++){
                if(str[i]==str[j]&&str[i+1]==str[j+1]&&str[i+2]==str[j+2]){
                    cout << "NG" <<endl;
                    flag=1;
                    flag2=1;
                    break;
                }
            }
            if(flag2) break;
        }
        if(flag == 0){
            cout << "OK" <<endl;
        }
    }       
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

12-19 16:52
门头沟学院
点赞 评论 收藏
分享
想干测开的tomca...:这份简历是“大一新生硬凹资深后端”的典型反面教材,槽点离谱到能让面试官直接笑出声: ### 1. 「年龄+入学时间」和项目复杂度完全脱节,可信度直接归0 你2024年7月才入学(现在刚读了1年多),19岁的大一新生,能把Vue3+Spring Boot+ShardingSphere+K8s+AI这些技术全塞进两个项目里?别说实际开发,光把这些技术的文档看完都得半年——这不是“能力强”,是“把招聘JD里的技术词全抄过来造假”,明摆着没碰过实际代码
点赞 评论 收藏
分享
ddd7_:跟我一模一样,加微信的hr都同一个,扫码了白年书人查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务