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

密码验证合格程序

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

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

bool check_first(string input) {
    if (input.size() >= 8) {
        return true;
    }
    return false;
}

bool check_sedcond(string input) {
    // 大写 小写 数字 其他 包括以上四种至少三种
    vector<bool> record(4, 0);
    for (char c : input) {
        if (c >= '0' && c <= '9')
            record[2] = 1;
        else if (c >= 'A' && c <= 'Z')
            record[0] = 1;
        else if (c >= 'a' && c <= 'z')
            record[1] = 1;
        else
            record[3] = 1;
    }
    int n = 0;
    for (bool d : record) {
        if (d)
            n++;
    }
    if (n >= 3) {
        return true;
    }
    return false;
}

bool check_third(string input) {
    for (int i = 0; i < input.size() - 1; i++) {
        for (int j = i + 1; j < input.size(); j++) {
            if (input[i] == input[j] && j <= input.size() - 3) {
                if (input.substr(i, 3) == input.substr(j, 3)) {
                    return false;
                }
            }
        }
    }
    return true;
}

int main() {
    string s;
    while (cin >> s) {
        // cout << "input is: " << s << endl;
        vector<bool> sl(4, 0);
        bool first = false;
        bool second = false;
        bool third = true;
        // sl is the list of standard;
        /*
        sl[0]: if contain upper letter
        sl[1]: if contian down letter
        sl[2]: if contain number;
        sl[3]: if contain other (except " " and "/n")
        */
        first = check_first(s);
        // cout << "The 1st rule is " << first << endl;
        second = check_sedcond(s);
        // cout << "The 2nd rule is " << second << endl;
        third = check_third(s);
        // cout << "The 3rd rule is " << third << endl;
        if (first && second && third) {
            cout << "OK" << endl;
        } else {
            cout << "NG" << endl;
        }
    }
}

非常直观的一个答案

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务