题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
#include <cctype>
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
int lengthPoint(string str){
int res = 0;
// 1. 判断长度
int len = str.length();
if(len <= 4){
res = 5;
}else if(len <= 7){
res = 10;
}else{
res = 25;
}
return res;
}
int cntAlphaPoint(string str){
int cnt = 0;
int upper = 0;
int lower = 0;
int res = 0;
for(auto it : str){
if(isalpha(it)){
cnt ++;
if(isupper(it)){
upper++;
}else{
lower++;
}
}
}
if(cnt == 0){
res = 0;
}else if(upper == 0 || lower == 0){
res = 10;
}else{
res = 20;
}
return res;
}
int cntDigitPoint(string str){
int cnt = 0;
int res = 0;
for(auto it : str){
if(isdigit(it)){
cnt ++;
}
}
if(cnt == 0){
res = 0;
}else if(cnt == 1){
res = 10;
}else{
res = 20;
}
return res;
}
int cntSimbolPoint(string str){
int cnt = 0;
int res = 0;
for(auto it : str){
if(ispunct(it)){
cnt ++;
}
}
if(cnt == 0){
res = 0;
}else if(cnt == 1){
res = 10;
}else{
res = 25;
}
return res;
}
int cntElse(int a, int b, int c, int d){
int res = 0;
if(b == 20 && c >= 10 && d >= 10){
res = 5;
}else if(b >= 10 && c >= 10 && d >= 10){
res = 3;
}else if(b >= 10 && c >= 10){
res = 2;
}
return res;
}
void printPoint(int total){
if(total >= 90){
cout << "VERY_SECURE" << endl;
}else if(total >= 80){
cout << "SECURE" << endl;
}else if(total >= 70){
cout << "VERY_STRONG" << endl;
}else if(total >= 60){
cout << "STRONG" << endl;
}else if(total >= 50){
cout << "AVERAGE" << endl;
}else if(total >= 25){
cout << "WEAK" << endl;
}else if(total >= 0){
cout << "VERY_WEAK" << endl;
}
}
int main() {
string str;
while (getline(cin, str)) { // 注意 while 处理多个 case
int a = 0, b = 0, c = 0, d = 0, e = 0, total = 0;
a = lengthPoint(str);
b = cntAlphaPoint(str);
c = cntDigitPoint(str);
d = cntSimbolPoint(str);
e = cntElse(a,b,c,d);
total = a+b+c+d+e;
printPoint(total);
}
}
// 64 位输出请用 printf("%lld")
