题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
#include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; int main() { string str; while (cin >> str) { // 注意 while 处理多个 case // 分数 int score = 0; // 长度 int len = str.length(); // 判断长度,加分 if (len <= 4) { score += 5; } else if (len >= 5 && len <= 7) { score += 10; } else { score += 25; } // 是否混合大小写 bool mixCh = 0; // 是否纯 大/小 写 bool singleCh = 0; // 有没有数字 bool isNum = 0; // 有没有符号 bool isOth = 0; // 放字母 string character; // 放数字 string num; // 放符号 string other; // 把原字符串分为三份存储,一份字母,一份数字,一份符号 for (int i = 0; i < len; i++) { if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z') { character += str[i]; } else if (str[i] >= '0' && str[i] <= '9') { num += str[i]; } else { other += str[i]; } } // 是否全大写 bool is_all_upper = all_of(character.begin(), character.end(), [](unsigned char c) { return std::isupper(c); }); // 是否全小写 bool is_all_lower = all_of(character.begin(), character.end(), [](unsigned char c) { return std::islower(c); }); // 查所有字母的情况,加分 if (character.length() == 0) { score += 0; } else if (is_all_upper || is_all_lower) { score += 10; singleCh = 1; } else { score += 20; mixCh = 1; } // 查所有数字的情况,加分 if (num.length() == 0) { score += 0; } else if (num.length() == 1) { isNum = 1; score += 10; } else if (num.length() > 1) { isNum = 1; score += 20; } // 查所有符号的情况,加分 if (other.length() == 0) { score += 0; } else if (other.length() == 1) { isOth = 1; score += 10; } else if (other.length() > 1) { isOth = 1; score += 25; } // 附加分 if (mixCh && isNum && isOth && (!singleCh)) { score += 5; } else if (singleCh && isNum && isOth && (!mixCh)) { score += 3; } else if (singleCh && isNum && (!isOth) && (!mixCh)) { 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 { cout << "VERY_WEAK" << endl; } return 0; } } // 64 位输出请用 printf("%lld")