题解 | #密码强度等级#

密码强度等级

https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361

#include <bits/stdc++.h>
#include <cctype>
using namespace std;

void CheckLength(std::string pwd, int &point) {
    auto len = pwd.size();
    if (len <= 4) {
        point += 5;
    } else if (len >= 5 && len <= 7) {
        point += 10;
    } else if (len >= 8) {
        point += 25;
    }
}

void CheckAlpha(std::string pwd, int &point) {
    int count = 0;
    int hasLower = 0;
    int hasUpper = 0;
    for (const char ch : pwd) {
        if (isalpha(ch)) {
            if (isupper(ch)) {
                hasUpper = 1;
            }
            if (islower(ch)) {
                hasLower = 1;
            }
        }
    }

    int ret = hasLower + hasUpper;

    if (ret == 0) {
        point += 0;
    } else if (ret == 1) {
        point += 10;
    } else if (ret == 2) {
        point += 20;
    }
}

void CheckDigit(std::string pwd, int &point) {
    int count = 0;
    int didgitCount = 0;
    for (const char ch : pwd) {
        if (isdigit(ch)) {
            didgitCount++;
        }
    }
    if (didgitCount == 0) {
        point += 0;
    } else if (didgitCount == 1) {
        point += 10;
    } else {
        point += 20;
    }
}

void CheckSymbol(std::string pwd, int &point) {
    int count = 0;
    int symbolCount = 0;
    for (const char ch : pwd) {
        if (ch >= 0x21 && ch <= 0x2f ||
            ch > 0x3A && ch <= 0X40 ||
            ch > 0x5B && ch <= 0X60 ||
            ch > 0x7B && ch <= 0X7E ) {
            symbolCount++;
        }
    }
    if (symbolCount == 0) {
        point += 0;
    } else if (symbolCount == 1) {
        point += 10;
    } else {
        point += 25;
    }
}

void CheckBonus(std::string pwd, int &point) {
    bool hasAlpha = false, hasLower = false, hasUpper = false, hasdigit = false, hasSymbol = false;
    for (const char ch : pwd) {
        if (isalpha(ch)) {
            hasAlpha = true;
            if (isupper(ch)) {
                hasUpper = true;
            }
            if (islower(ch)) {
                hasLower = true;
            }
        }
        if (isdigit(ch)) {
            hasdigit = true;
        }
        if ((ch >= 0x21 && ch <= 0x2F) ||
            (ch >= 0x3A && ch <= 0X40) ||
            (ch >= 0x5B && ch <= 0X60) ||
            (ch >= 0x7B && ch <= 0X7E) ) {
            hasSymbol = true;
        }
    }
    if (hasUpper && hasLower && hasdigit && hasSymbol) {
        point += 5;
        return;
    } 
     if (hasAlpha && hasdigit && hasSymbol) {
        point += 3;
        return;
    }
    if (hasAlpha && hasdigit) {
        point += 2;
        return;
    }
}

void PrintResult(int point) {
    string res;
    if (point >= 90) {
        res = "VERY_SECURE";
    } else if (point >= 80 && point < 90) {
        res = "SECURE";
    } else if (point >= 70 && point < 80) {
        res = "VERY_STRONG";
    } else if (point >= 60 && point < 70) {
        res = "STRONG";
    } else if (point >= 50 && point < 60) {
        res = "AVERAGE";
    } else if (point >= 25 && point < 50) {
        res = "WEAK";
    } else {
        res = "VERY_WEAK";
    }
    cout << res << endl;
}

void foo(string& pwd, int point) {
    CheckLength(pwd, point);
    CheckAlpha(pwd, point);
    CheckDigit(pwd, point);
    CheckSymbol(pwd, point);
    CheckBonus(pwd, point);
    PrintResult(point);
}

int main() {
    int point = 0;
    string pwd;
    while (getline(cin, pwd)) {
        foo(pwd, point);
    }
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 15:58
投个小米提前批试试水,先投一个岗位看看形势,不行就再沉淀一下投第二个岗位,莫辜负
Java抽象带篮子:我嘞个骚刚,已经开始研发6g了吗
投递小米集团等公司7个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 14:00
不想多说了,什么逆天HR,还要教我礼貌😂
机械打工仔:这不纯傻卵吗,他还操心上别人老板了
投递BOSS直聘等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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