题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
// HJ87 密码强度等级.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int judge_length(string s)
{
int score = 0;
int len = s.size();
if (len <= 4)
{
score = 5;
}
else if (len >= 5 && len <= 7)
{
score = 10;
}
else
{
score = 25;
}
//cout << score << endl;
return score;
}
int judge_alpha(string s)
{
int score = 0;
int capital = 0;
int lower = 0;
for (int i = 0; i < s.size(); i++)
{
if (isalpha(s[i]))
{
if (isupper(s[i]))
{
capital++;
}
else if (islower(s[i]))
{
lower++;
}
}
}
if (capital > 0&&lower>0)
{
score = 20;
}
else if (capital > 0 || lower > 0)
{
score = 10;
}
else
{
score = 0;
}
//cout << score << endl;
return score;
}
int judge_digit(string s)
{
int score = 0;
int count = 0;
for (int i = 0; i < s.size(); i++)
{
if (isdigit(s[i]))
{
count++;
}
}
if (count == 0)
{
score = 0;
}
else if (count == 1)
{
score = 10;
}
else
{
score = 20;
}
//cout << score << endl;
return score;
}
int judge_symbol(string s)
{
int score = 0;
int alpha = 0;
int digit = 0;
int count = 0;
int len = s.size();
for (int i = 0; i < len; i++)
{
if (isalpha(s[i]))
{
alpha++;
}
else if (isdigit(s[i]))
{
digit++;
}
else
{
count++;
}
}
if (count == 0)
{
score = 0;
}
else if (count == 1)
{
score = 10;
}
else
{
score = 25;
}
//cout << score << endl;
return score;
}
int judge_award(string s)
{
int score = 0;
int alpha = 0;
int big_alpha = 0, s_alpha = 0;
int digit = 0;
int count = 0;
for (int i = 0; i < s.size(); i++)
{
if (isalpha(s[i])&& isupper(s[i]))
{
alpha++; big_alpha++;
}
else if (isdigit(s[i]))
{
digit++;
}
else if (isalpha(s[i]) && islower(s[i]))
{
s_alpha++;
}
else
{
count++;
}
}
if(big_alpha > 0 && s_alpha > 0 && digit > 0 && count > 0)
{
score = 5;
}
else if (alpha > 0 && digit > 0 && count > 0)
{
score = 3;
}
else if (alpha > 0 && digit > 0)
{
score = 2;
}
//cout << score << endl;
return score;
}
int main()
{
string s;
while (cin >> s)
{
int score = 0;
score = judge_length(s) + judge_alpha(s) + judge_digit(s) + judge_symbol(s) + judge_award(s);
//cout << score << endl;
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;
}