题解 | #密码强度等级#

密码强度等级

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

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

string flat(int num)
{
    if (num >= 90) return "VERY_SECURE";
    else if (num < 90 && num >= 80) return "SECURE";
    else if (num < 80 && num >= 70) return "VERY_STRONG";
    else if (num < 70 && num >= 60) return "STRONG";
    else if (num < 60 && num >= 50) return "AVERAGE";
    else if (num < 50 && num >= 25) return "WEAK";
    else if (num < 25 && num >= 0)  return "VERY_WEAK";
    else {return "ERROR";}
}

int ComputeScore(string s)
{
    int score1 = 0, score2 = 0, score3 = 0, score4 = 0, score5 = 0;
    for (int i = 0; i < s.length(); i++){
        if (score1 < 10 && s[i] >= 'a' && s[i] <= 'z'){
            score1 = 10;
            continue;
        }
        
        if (score2 < 10 && s[i] >= 'A' && s[i] <= 'Z'){
            score2 = 10;
            continue;
        }

        if (score3 < 20 && s[i] >= '0' && s[i] <= '9' ){
            score3 += 10;
            continue;
        }

        if (score4 < 25 && (s[i] >= 0x21 && s[i] <= 0x2F) 
            || (s[i] >= 0x3A && s[i] <= 0x40)
            || (s[i] >= 0x5B && s[i] <= 0x60)
            || (s[i] >= 0x7B && s[i] <= 0x7E))
        {
            if (score4 < 10) score4 += 10;
            else {score4 = 25;}
            continue;
        }
    }
    if ((score1 != 0 || score2 != 0) && score3 != 0){
        score5 += 2;
        if (score4 != 0){
            score5 += 1;
            if (score1 != 0 && score2 != 0){
                score5 += 2;
            }
        }
    }
    // cout << score1 << ","<< score2 << ","  << score3  << "," << score4 << ","  << score5 <<endl;
    // cout << ans <<endl;
    return score1 + score2 + score3 + score4 + score5;
}

int main() {
    string input,ans;
    int score = INT_MIN;
    while (cin >> input){
        int len = input.length();

        if (len <= 4)                 score = 5;
        else if (len > 4 && len <= 7) score = 10;
        else if (len > 7)             score = 25;
        else {}
        // cout <<score <<endl;

        int num = ComputeScore(input);
        ans = flat(score +  num);
    }
    cout << ans << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
11-18 09:44
Java
小白也想要offer:简历别放洋屁,搞不还还放错了,当然你投外企除外,以上纯属个人观点
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务