题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
#include <iostream>
using namespace std;
//接口一、密码长度:5 分: 小于等于4 个字符;10 分: 5 到7 字符;25 分: 大于等于8 个字符
int Password_Length_Score(int len)
{
int score = 0;
if ((len >= 0) && (len <= 4)) {
score = 5;
} else if ((len >= 5) && (len <= 7)) {
score = 10;
} else if (len >= 8) {
score = 25;
}
return score;
}
//接口二、字母: 0 分: 没有字母;10 分: 全都是小(大)写字母;20 分: 大小写混合字母
int Alphabet_Score(int num1, int num2)
{
//大写字母:65~90;小写字母:97~122
int score = 0;
if ((num1 == 0) && (num2 == 0)) {
score = 0;
} else if (num1 * num2 == 0) {
score = 10;
} else if (num1 * num2 > 0) {
score = 20;
}
return score;
}
//接口三、数字:0 分: 没有数字;10 分: 1 个数字;20 分: 大于1 个数字
int Number_Score(int num)
{
int score = 0;
if (num == 0) {
score = 0;
} else if (num == 1) {
score = 10;
} else if (num > 1) {
score = 20;
}
return score;
}
//接口四、符号:0 分: 没有符号;10 分: 1 个符号;25 分: 大于1 个符号
int Symbol_Score(int num)
{
int score = 0;
if (num == 0) {
score = 0;
} else if (num == 1) {
score = 10;
} else if (num > 1) {
score = 25;
}
return score;
}
//接口五、奖励:2 分: 字母和数字;3 分: 字母、数字和符号;5 分: 大小写字母、数字和符号
int Reward_Score(int score1, int score2, int score3)
{
int score = 0;
if ((score1 != 0) && (score2 != 0) && (score3 == 0)) {
score = 2;
} else if ((score1 == 10) && (score2 != 0) && (score3 != 0)) {
score = 3;
} else if ((score1 == 20) && (score2 != 0) && (score3 != 0)) {
score = 5;
}
return score;
}
//获取密码强度等级的函数接口
int Get_Password_Security_Level(string PasswordStr)
{
int score[4] = {0}; //密码得分
int Array[4] = {0}; //字符记录数组
for (int i = 0; i < PasswordStr.size(); i++) {
if (PasswordStr[i] >= 'a' && PasswordStr[i] <= 'z') {
++Array[0];
} else if (PasswordStr[i] >= 'A' && PasswordStr[i] <= 'Z') {
++Array[1];
} else if (PasswordStr[i] >= '0' && PasswordStr[i] <= '9') {
++Array[2];
} else {
++Array[3];
}
}
//累计各项得分
score[0] += Password_Length_Score(PasswordStr.size() - 1);
score[1] = Alphabet_Score(Array[0],Array[1]);
score[2] = Number_Score(Array[2]);
score[3] = Symbol_Score(Array[3]);
score[0] += Reward_Score(score[1], score[2], score[3]) + score[1] + score[2] + score[3];
if ((score[0] >= 90) && (score[0] <= 100)) {
cout << "VERY_SECURE" << endl;
} else if ((score[0] >= 80) && (score[0] < 90)) {
cout << "SECURE" << endl;
} else if ((score[0] >= 70) && (score[0] < 80)) {
cout << "VERY_STRONG" << endl;
} else if ((score[0] >= 60) && (score[0] < 70)) {
cout << "STRONG" << endl;
} else if ((score[0] >= 50) && (score[0] < 60)) {
cout << "AVERAGE" << endl;
} else if ((score[0] >= 25) && (score[0] < 50)) {
cout << "WEAK" << endl;
} else if ((score[0] >= 0) && (score[0] < 25)) {
cout << "VERY_WEAK" << endl;
}
return 0;
}
//主函数
int main()
{
string PasswordStr;
while (getline(cin, PasswordStr)) {
Get_Password_Security_Level(PasswordStr);
}
return 0;
}
using namespace std;
//接口一、密码长度:5 分: 小于等于4 个字符;10 分: 5 到7 字符;25 分: 大于等于8 个字符
int Password_Length_Score(int len)
{
int score = 0;
if ((len >= 0) && (len <= 4)) {
score = 5;
} else if ((len >= 5) && (len <= 7)) {
score = 10;
} else if (len >= 8) {
score = 25;
}
return score;
}
//接口二、字母: 0 分: 没有字母;10 分: 全都是小(大)写字母;20 分: 大小写混合字母
int Alphabet_Score(int num1, int num2)
{
//大写字母:65~90;小写字母:97~122
int score = 0;
if ((num1 == 0) && (num2 == 0)) {
score = 0;
} else if (num1 * num2 == 0) {
score = 10;
} else if (num1 * num2 > 0) {
score = 20;
}
return score;
}
//接口三、数字:0 分: 没有数字;10 分: 1 个数字;20 分: 大于1 个数字
int Number_Score(int num)
{
int score = 0;
if (num == 0) {
score = 0;
} else if (num == 1) {
score = 10;
} else if (num > 1) {
score = 20;
}
return score;
}
//接口四、符号:0 分: 没有符号;10 分: 1 个符号;25 分: 大于1 个符号
int Symbol_Score(int num)
{
int score = 0;
if (num == 0) {
score = 0;
} else if (num == 1) {
score = 10;
} else if (num > 1) {
score = 25;
}
return score;
}
//接口五、奖励:2 分: 字母和数字;3 分: 字母、数字和符号;5 分: 大小写字母、数字和符号
int Reward_Score(int score1, int score2, int score3)
{
int score = 0;
if ((score1 != 0) && (score2 != 0) && (score3 == 0)) {
score = 2;
} else if ((score1 == 10) && (score2 != 0) && (score3 != 0)) {
score = 3;
} else if ((score1 == 20) && (score2 != 0) && (score3 != 0)) {
score = 5;
}
return score;
}
//获取密码强度等级的函数接口
int Get_Password_Security_Level(string PasswordStr)
{
int score[4] = {0}; //密码得分
int Array[4] = {0}; //字符记录数组
for (int i = 0; i < PasswordStr.size(); i++) {
if (PasswordStr[i] >= 'a' && PasswordStr[i] <= 'z') {
++Array[0];
} else if (PasswordStr[i] >= 'A' && PasswordStr[i] <= 'Z') {
++Array[1];
} else if (PasswordStr[i] >= '0' && PasswordStr[i] <= '9') {
++Array[2];
} else {
++Array[3];
}
}
//累计各项得分
score[0] += Password_Length_Score(PasswordStr.size() - 1);
score[1] = Alphabet_Score(Array[0],Array[1]);
score[2] = Number_Score(Array[2]);
score[3] = Symbol_Score(Array[3]);
score[0] += Reward_Score(score[1], score[2], score[3]) + score[1] + score[2] + score[3];
if ((score[0] >= 90) && (score[0] <= 100)) {
cout << "VERY_SECURE" << endl;
} else if ((score[0] >= 80) && (score[0] < 90)) {
cout << "SECURE" << endl;
} else if ((score[0] >= 70) && (score[0] < 80)) {
cout << "VERY_STRONG" << endl;
} else if ((score[0] >= 60) && (score[0] < 70)) {
cout << "STRONG" << endl;
} else if ((score[0] >= 50) && (score[0] < 60)) {
cout << "AVERAGE" << endl;
} else if ((score[0] >= 25) && (score[0] < 50)) {
cout << "WEAK" << endl;
} else if ((score[0] >= 0) && (score[0] < 25)) {
cout << "VERY_WEAK" << endl;
}
return 0;
}
//主函数
int main()
{
string PasswordStr;
while (getline(cin, PasswordStr)) {
Get_Password_Security_Level(PasswordStr);
}
return 0;
}