题解 | 密码强度等级

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

int main() {
    string password;
    getline(cin, password);

    int score = 0;
    // 一、密码长度:
    // 5 分: 小于等于4 个字符
    // 10 分: 5 到7 字符
    // 25 分: 大于等于8 个字符
    int len_pw = password.size();
    if (len_pw <= 4) {
        score += 5;
    } else if (len_pw >= 5 && len_pw <= 7) {
        score += 10;
    } else if (len_pw >= 8) {
        score += 25;
    } else {
        ;
    }

    int upper_flag = 0;
    int lower_flag = 0;
    int num_flag = 0;
    int other_flag = 0;
    for (char ch : password) {
        if (ch >= 'A' && ch <= 'Z') {
            upper_flag ++;
        } else if (ch >= 'a' && ch <= 'z') {
            lower_flag ++;
        } else if (ch >= '0' && ch <= '9') {
            num_flag ++;
        } else {
            other_flag ++;
        }
    }

    // 二、字母:
    // 0 分: 没有字母
    // 10 分: 密码里的字母全都是小(大)写字母
    // 20 分: 密码里的字母符合”大小写混合“
    if (upper_flag == 0 && lower_flag == 0) {
        score += 0;
    } else if ((upper_flag != 0 && lower_flag == 0) || 
			   (upper_flag == 0 && lower_flag != 0)) {
        score += 10;
    } else {
        score += 20;
    }

    // 三、数字:
    // 0 分: 没有数字
    // 10 分: 1 个数字
    // 20 分: 大于1 个数字
    if (num_flag == 0) {
        score += 0;
    } else if (num_flag == 1) {
        score += 10;
    } else {
        score += 20;
    }

    // 四、符号:
    // 0 分: 没有符号
    // 10 分: 1 个符号
    // 25 分: 大于1 个符号
    if (other_flag == 0) {
        score += 0;
    } else if (other_flag == 1) {
        score += 10;
    } else {
        score += 25;
    }

    // 五、奖励(只能选符合最多的那一种奖励):
    // 2 分: 字母和数字
    // 3 分: 字母、数字和符号
    // 5 分: 大小写字母、数字和符号
    if ( (upper_flag != 0 && lower_flag == 0 && num_flag != 0 && other_flag == 0) ||
            (upper_flag == 0 && lower_flag != 0 && num_flag != 0 && other_flag == 0) ||
            (upper_flag != 0 && lower_flag != 0 && num_flag != 0 && other_flag == 0)) {
        score += 2;
    } else if ((upper_flag != 0 && lower_flag == 0 && num_flag != 0 && other_flag != 0) ||
               (upper_flag == 0 && lower_flag != 0 && num_flag != 0 && other_flag != 0)) {
        score += 3;
    } else if ((upper_flag != 0 && lower_flag != 0 && num_flag != 0 && other_flag != 0)) {
        score += 5;
    }

    // 最后的评分标准:
    // >= 90: 非常安全
    // >= 80: 安全(Secure)
    // >= 70: 非常强
    // >= 60: 强(Strong)
    // >= 50: 一般(Average)
    // >= 25: 弱(Weak)
    // >= 0:  非常弱(Very_Weak)

    // 对应输出为:

    // VERY_SECURE
    // SECURE
    // VERY_STRONG
    // STRONG
    // AVERAGE
    // WEAK
    // VERY_WEAK
    if (score >= 90) {
        cout << "VERY_SECURE" << endl;
    } else if (score >= 80) {
        cout << "SECURE" << endl;
    } else if (score >= 70) {
        cout << "VERY_STRONG" << endl;
    } else if (score >= 60) {
        cout << "STRONG" << endl;
    } else if (score >= 50) {
        cout << "AVERAGE" << endl;
    } else if (score >= 25) {
        cout << "WEAK" << endl;
    } else {
        cout << "VERY_WEAK" << endl;
    }

    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 11:55
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-11 11:24
大家还是用ai改吧,我心疼得要死,就当花钱买教训吧,人家直接拿完钱就跑路了
程序员小白条:简历修改700....神奇,又不是帮你面试,咋的,简历修改从双非变92了还是没实习变成有大厂实习了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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