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

密码验证合格程序

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

#include <iostream>
#include <ctype.h>
#include <set>

using namespace std;

string ans(string s){
    if(s.size()<=8)
        return "NG";

    //cout << "!" << endl;

    int tag[4] = {0};
    int flag = 0;
    for(const char &c:s){
        if(isupper(c))  //? int ispunct(int c);  <ctype.h> 返回非零值(true),否则返回 0(false)
            tag[0] = 1;
        if(islower(c))
            tag[1] = 1;
        if(isdigit(c))
            tag[2] = 1;
        if(ispunct(c))
            tag[3] = 1;
    }
    for(const int &i:tag)   //数组元素也可以这样操作
        flag+=i;
    if(flag<3)
        return "NG";

    //cout << "!!" << endl;

    set<string> str;    //需要头文件
    int size = s.size();
    for(int i = 0;i<=size-3;i++){
        string tem = s.substr(i, 3);
        if(str.find(tem)!=str.end())
            return "NG";
        else str.insert(tem);

    }

    return "OK";

}

int main(){
    string s;

    while(cin >> s){
        cout << ans(s) << endl; //多用例注意输入输出
    }
}

全部评论

相关推荐

我已成为0offer的糕手:走算法要发论文的,至少你简历上一篇没有,这个薪资估计没戏了,实习和论文都没有,你不如先考虑考虑算法这条路,会不会因为本科学历把你的简历直接给刷了,转开发吧
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务