题解 | #密码验证合格程序#

密码验证合格程序

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

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

bool elemTypes(string s) {
    vector<int> t(4, 0);
    for (char c: s) {
        if (c >= 'a' && c <= 'z') {
            t[0] = 1;
        } else if (c >= 'A' && c <= 'Z') {
            t[1] = 1;
        } else if (c >= '0' && c <= '9') {
            t[2] = 1;
        } else {
            t[3] = 1;
        }
    }
    return (t[0] + t[1] + t[2] + t[3] >= 3);
}

bool elemRep(string s) {
    for (int i = 0; i < s.size() - 2; i++) {
        for (int j = i + 3; j < s.size() - 2; j++) {
            if (s[j] == s[i] && s[j + 1] == s[i + 1] && s[j + 2] == s[i + 2]) {
                return false;
            }
        }
    }
    return true;
}

int main() {
    string s;
    while (getline(cin, s)) { // 注意 while 处理多个 case
        // length
        if (s.size() < 8) {
            cout << "NG" << endl;
            continue;
        }

        // 3 types
        if (!elemTypes(s)) {
            cout << "NG" << endl;
            continue;
        }

        // repeatation
        if (!elemRep(s)) {
            cout << "NG" << endl;
            continue;
        }

        cout << "OK" << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-24 18:34
点赞 评论 收藏
分享
Gaynes:查看图片
点赞 评论 收藏
分享
昨天 14:37
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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