题解 | #密码强度等级#__huawei_no.87-1

密码强度等级

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

#include<iostream>
#include<string>

using namespace std;

class PW {
  private:
    string password;
    int upper = 0;
    int lower = 0;
    int digit = 0;
    int symbol = 0;

  public:
    void init(string s) {
        this->password = s;
        for (int i = 0; i < s.size();
                i++) { //统计大小写字母、数字、符号出现的次数
            if (islower(s[i])) { //小写字母
                this->lower++;
            } else if (isupper(s[i])) { //大写字母
                this->upper++;
            } else if (isdigit(s[i])) { //数字
                this->digit++;
            } else { //符号
                this->symbol++;
            }
        }
    }

    int scoreOfLen() { //计算长度得分
        if (this->password.size() <= 4)
            return 5;
        else if (this->password.size() <= 7)
            return 10;
        return 25;
    }

    int scoreOfAlpha() { //计算字母得分
        if (this->upper == 0 || this->lower == 0) {
            return 10;
        } else if (this->upper > 0 && this->lower > 0) {
            return 20;
        }
        return 0;
    }

    int scoreOfDigit() { //计算数字得分
        if (this->digit == 1)
            return 10;
        else if (this->digit > 1)
            return 20;
        return 0;
    }

    int scoreOfSymbol() { //计算符号得分
        if (this->symbol == 1) {
            return 10;
        } else if (this->symbol > 1) {
            return 25;
        }
        return 0;
    }

    int award() { //计算奖励分数
        if (this->upper > 0 && this->lower > 0 && this->digit > 0 &&
                this->symbol > 0) //同时有大小写字母、数字、符号
            return 5;
        else if (this->upper + this->lower > 0 && this->digit > 0 &&
                 this->symbol > 0) //有字母、数字、符号
            return 3;
        else if (this->upper + this->lower > 0 && this->digit > 0) //有字母、数字
            return 2;
        return 0;
    }
};


int main() {
    string s;
    while (cin >> s) {
        PW str;
        str.init(s);
        int score = str.award() + str.scoreOfLen() + str.scoreOfAlpha() +
                    str.scoreOfDigit() + str.scoreOfSymbol();//计算总分
        //按照分数输出对应的密码强度等级
        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;
}



纯粹的暴力

全部评论

相关推荐

迷茫的大四🐶:好一个误闯天家,我也想闯一闯
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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