题解 | #密码强度等级#
密码强度等级
http://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
描述
密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分。
一、密码长度:
5 分: 小于等于4 个字符
10 分: 5 到7 字符
25 分: 大于等于8 个字符
二、字母:
0 分: 没有字母
10 分: 全都是小(大)写字母
20 分: 大小写混合字母
三、数字:
0 分: 没有数字
10 分: 1 个数字
20 分: 大于1 个数字
四、符号:
0 分: 没有符号
10 分: 1 个符号
25 分: 大于1 个符号
五、奖励:
2 分: 字母和数字
3 分: 字母、数字和符号
5 分: 大小写字母、数字和符号
最后的评分标准:
= 90: 非常安全
= 80: 安全(Secure)
= 70: 非常强
= 60: 强(Strong)
= 50: 一般(Average)
= 25: 弱(Weak)
= 0: 非常弱
对应输出为:
VERY_SECURE
SECURE,
VERY_STRONG,
STRONG,
AVERAGE,
WEAK,
VERY_WEAK,
请根据输入的密码字符串,进行安全评定。
输入描述:
本题含有多组输入样例。
每组样例输入一个string的密码
输出描述:
每组样例输出密码等级
示例1
输入:
38$@NoNoNo
123
复制
输出:
VERY_SECURE
WEAK
复制
说明:
第一组样例密码强度为95分。
第二组样例密码强度为25分。
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
while(getline(cin, s)){
int score = 0;
int num_lower=0, num_high=0, num_n=0, num_s=0;
int len = s.length();
// 密码长度
if(len<5)
score += 5;
else if(len<8)
score += 10;
else
score += 25;
//统计输入字符中大小写字母、数字、符号的个数
for(int i=0; i<len; i++){
if(s[i]>=65&&s[i]<=90)
num_high++;
else if(s[i]>=97&&s[i]<=122)
num_lower++;
else if(s[i]>=48&&s[i]<=57)
num_n++;
else
num_s++;
}
//字母
if(num_lower>0&&num_high>0)
score += 20;
else if(num_lower!=0 || num_high !=0)
score += 10;
//数字
if(num_n==1)
score += 10;
else if(num_n>1)
score += 20;
//符号
if(num_s==1)
score += 10;
else if(num_s>1)
score += 25;
//奖励
if(num_lower!=0&&num_high!=0&&num_s!=0&&num_n!=0)
score += 5;
else if((num_lower!=0||num_high!=0)&&num_s!=0&&num_n!=0)
score += 3;
else if((num_lower!=0||num_high!=0)&&num_s==0&&num_n!=0)
score += 2;
//评分标准
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 if(score>=0){cout<<"VERY_WEAK"<<endl;}
}
return 0;
}