题解 | 密码验证合格程序

密码验证合格程序

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

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

int main() {
    string str;
    int sublen=0;
    while(getline(cin,str)){
        bool flag=0;
        int comp[4]={0,0,0,0};
        int len =str.size();    //字符长度
        if(len<8) {  //长度超过8
            cout << "NG" <<endl;
            flag=1;
            continue;
        }
        for(int i=0;i<len;i++){
            if(str[i] >='A' && str[i] <= 'Z') {
                comp[0] =1;
                continue;
            }
            if(str[i] >='a' && str[i] <= 'z') {
                comp[1] =1;
                continue;
            }
            if(str[i] >='0' && str[i] <= '9') {
                comp[2] =1;
                continue;
            }
            if(str[i] >='!' && str[i] <= '/') {
                comp[3] =1;
                continue;
            }else if(str[i] >=':' && str[i] <= '@') {
                comp[3] =1;
                continue;
            }else if(str[i] >='[' && str[i] <= '`') {
                comp[3] =1;
                continue;
            }else if(str[i] >='{' && str[i] <= '~') {
                comp[3] =1;
                continue;
            }
        }
        //至少三种字符
        int n = sizeof(comp) / sizeof(comp[0]); // 计算数组长度
        int target = 1;
        int count = std::count(comp, comp + n, target); // 统计元素出现次数
        if(count <3) {  
            cout <<"NG" <<endl;
            flag=1;
            continue;
        }
        //不重复的连续字串
        bool flag2 =0;
        for(int i=0;i<len-5;i++){
            for(int j=i+1;j<len-2;j++){
                if(str[i]==str[j]&&str[i+1]==str[j+1]&&str[i+2]==str[j+2]){
                    cout << "NG" <<endl;
                    flag=1;
                    flag2=1;
                    break;
                }
            }
            if(flag2) break;
        }
        if(flag == 0){
            cout << "OK" <<endl;
        }
    }       
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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