题解 | #密码强度等级#

密码强度等级

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

感觉最坑的还是大小写字母那一块(加10分那一条),容易错误解读,是已出现的字母是否是全为大/小写,而不是整个密码串全为大/小写字母!!!!!

#include <iostream>
using namespace std;
int main(){
    string code;   //密码
    int score;   //密码强度得分
    string grade;   //密码强度对应的等级
    while(cin>>code){
        score = 0;
        //1、长度得分
        int len = code.length();    //密码长度
        if(len <= 4) score +=5;
        else  if(len >=5 && len <= 7) score +=10;
        else  if(len >=8 ) score +=25;

        //2、字母得分
        bool letter_flag = false;     //字母有无
        bool low_high_letter_flag = false;    //是否同时有大小写字母
        int low_letter_cnt = 0;     //密码小写字母数量
        int high_letter_cnt = 0;     //密码大写字母数量

        //3、数字得分
        bool number_flag = false;    //数字有无
        int number_cnt = 0;    //密码数字数量

        //4、符号得分
        bool sign_flag = false;    //符号有无
        int sign_cnt = 0;     //符号数量

        //逐个字符判断
        for(int i = 0; i < len; i++){
            //字母判断
            if(code[i] >= 'a' && code[i] <= 'z'){
                low_letter_cnt ++;
            }else if(code[i] >= 'A' && code[i] <= 'Z'){
                high_letter_cnt ++;
            }
            //数字判断
            else if(code[i] >= '0' && code[i] <= '9'){
                number_cnt ++;
            }
            //符号判断
             else if(code[i] >= '!' && code[i] <= '/' || code[i] >= ':' && code[i] <= '@'
              || code[i] >= '[' && code[i] <= '`' || code[i] >= '{' && code[i] <= '~'){
                sign_cnt ++;
            }
        }
        //循环结束以后
        //字母判断
        if(low_letter_cnt || high_letter_cnt) letter_flag = true;    //说明有字母
        //下面这行感觉最容易让人产生误解
        if(low_letter_cnt && !high_letter_cnt || high_letter_cnt && !low_letter_cnt) score += 10;
        else if(low_letter_cnt && high_letter_cnt){
            score += 20;
            low_high_letter_flag = true;
        } 
        //数字判断 
        if(number_cnt == 1) {
            score += 10;
            number_flag = true;
        }else if(number_cnt > 1){
            score += 20;
            number_flag = true;
        } 

        //符号判断
        if(sign_cnt == 1) {
            score += 10;
            sign_flag = true;
        }else if(sign_cnt > 1){
            score += 25;
            sign_flag = true;
        } 

        //5、奖励得分
        if(low_high_letter_flag && number_flag && sign_flag) score += 5;
        else if(letter_flag && number_flag && sign_flag ) score += 3;
        else if(letter_flag && number_flag) score += 2;

        //输出对应等级
        if(score >= 90) grade = "VERY_SECURE";
        else if(score >= 80) grade = "SECURE";
        else if(score >= 70) grade = "VERY_STRONG";
        else if(score >= 60) grade = "STRONG";
        else if(score >= 50) grade = "AVERAGE";
        else if(score >= 25) grade = "WEAK";
        else if(score >= 0) grade = "VERY_WEAK";
        cout << grade << endl;
    }
    return 0;
}




全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务