题解 | 密码验证合格程序

密码验证合格程序

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

#include <iostream>
#include <string>
#include <set>

using namespace std;

bool check(string pwd) {
    if (pwd.length() < 8) { 	// 题目应是想说 长度不低于8
        return false;
    }
    int spc[4] = {0, 0, 0, 0};  // 大写/小写/数字/特殊字符
    for (auto elem : pwd) {
        if (elem >= 'A' && elem <= 'Z')
            spc[0] = 1;
        else if (elem >= 'a' && elem <= 'z')
            spc[1] = 1;
        else if (elem >= '0' && elem <= '9')
            spc[2] = 1;
        else if (elem >= 33 && elem <= 126) {   // 其他特殊字符
            spc[3] = 1;
        }
    }
    // 计算字符种类
    int cnt = 0;
    for (int i = 0; i <= 3; ++i) cnt += spc[i];
    if (cnt < 3) {
        return false;
    }
    // 判断是否存在长度>= 3的重复的独立字串
    int len = pwd.length();
    for (int i = 0; i <= len - 3; ++i) {
        string s1 = pwd.substr(i, 3);
        for (int j = i + 3; j <= len - 3; ++j) {
            string s2(pwd.substr(j, 3));
            if (s1 == s2)    return false;
        }
    }
    return true;
}

int main() {
    string passwd;
    while (cin >> passwd) {
        if (check(passwd))   cout << "OK" << endl;
        else    cout << "NG" << endl;
    }
    return 0;
}

全部评论

相关推荐

大世界中的渺小一棵:看出来你软硬都有基础,但是这样写简历软硬都擦边不知道你想投什么,建议针对岗位jd针对性修改下。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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