题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
#include <iostream> #include <string> using namespace std; //密码长度 int len(const string s) { if (s.length() <= 4) return 5; if (s.length() >= 8) return 25; return 10; } //字母 int letters(const string s) { int grade = 0; //如果有小写字母,加十分 for (int i = 0; i < s.length(); i++) if (('a' <= s[i]) && (s[i] <= 'z')) { grade += 10; break; } //如果有大写字母,加十分 for (int i = 0; i < s.length(); i++) if (('A' <= s[i]) && (s[i] <= 'Z')) { grade += 10; break; } return grade; } //数字 int digits(const string s) { int grade = 0; //每个数字记十分 for (int i = 0; i < s.length(); i++) if (('0' <= s[i]) && (s[i] <= '9')) grade += 10; //超过20分,记20分 if (grade > 20) return 20; else return grade; } //符号 int symbol(const string s) { int grade = 0; //每个符号记十分 for (int i = 0; i < s.length(); i++) { if ((0x21 <= s[i]) && (s[i] <= 0x2f)) grade += 10; if ((0x3A <= s[i]) && (s[i] <= 0x40)) grade += 10; if ((0x5B <= s[i]) && (s[i] <= 0x60)) grade += 10; if ((0x7B <= s[i]) && (s[i] <= 0x7E)) grade += 10; } //超过十分,记25分 if (grade > 10) return 25; else return grade; } //奖励 int reward(const int score1, const int score2, const int score3, const int score4) { //没有数字 if (score3 == 0) return 0; //没有字母 if (score2 == 0) return 0; //没有符号,但有字母和数字 if (score4 == 0) return 2; //字母分大小写,且有数字和符号 if (score2 == 20) return 5; //字母只有大写或小写,且有数字和符号 return 3; } //统计总分 int score(const string s) { int scores = 0; int score1 = len(s); scores += score1; int score2 = letters(s); scores += score2; int score3 = digits(s); scores += score3; int score4 = symbol(s); scores += score4; int score5 = reward(score1, score2, score3, score4); scores += score5; return scores; return 0; } //计分并划分等级 void test(const string s) { int scores = score(s); //划分等级 switch (scores / 10) { case 9: cout << "VERY_SECURE" << endl; return; case 8: cout << "SECURE" << endl; return; case 7: cout << "VERY_STRONG" << endl; return; case 6: cout << "STRONG" << endl; return; case 5: cout << "AVERAGE" << endl; return; default: if (scores >= 25) { cout << "WEAK" << endl; return; } else { cout << "VERY_WEAK" << endl; return; } } } int main() { int a, b; string s; while (cin >> s) { // 注意 while 处理多个 case test(s); } } // 64 位输出请用 printf("%lld")