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

密码验证合格程序

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

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

bool checkString(const string& a) {
    int isnum = 0;
    int isupper = 0;
    int islower = 0;
    int isother = 0;
    int finder = 0;

    if (a.length() <= 8) {
        return false;
    }

    for (string::const_iterator it = a.begin(); it != a.end(); it++) {
        if (isalpha(*it)) {
            if (abs(*it) >= 97 && abs(*it) <= 123) {
                islower = 1;
            } else if (abs(*it) >= 65 && abs(*it) <= 91) {
                isupper = 1;
            }
        } else if (isdigit(*it)) {
            isnum = 1;
        } else if (*it != ' ' && *it != '\n') {
            isother = 1;
        }
    }

    if (isnum + islower + isupper + isother < 3) {
        return false;
    }

    for (int i = 0; i <= a.size() - 3; i++) {
        string temp = a.substr(i, 3);
        string temp1 = a.substr(0, i);
        string temp2 = a.substr(i + 3);
        string temp3 = temp1 + temp2;
        finder = temp3.find(temp);
        if (finder != string::npos) {
            return false;
        }
    }

    return true;
}

int main() {
    string a;
    while (getline(cin, a)) {
        if (checkString(a)) {
            cout << "OK" << endl;
        } else {
            cout << "NG" << endl;
        }
    }
    return 0;
}

全部评论

相关推荐

10-05 23:02
东北大学 Java
我说句实话啊:那时候看三个月培训班视频,随便做个项目背点八股,都能说3 40w是侮辱价
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务