题解 | #密码强度等级# C++
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
#include <iostream> #include <cctype> using namespace std; int find_c (string str, int (*func)(int c)) { int count = 0; for (char c : str) { if (func(c)) { ++count; } } return count; } int issymbol(int c) { if ((c >= 0x21 && c <=0x2F) || (c >= 0x3A && c <= 0x40) || (c >= 0x5B && c <= 0x60) || (c >= 0x7B && c <= 0x7E) ) { return 1; } return 0; } int main() { string passwd; cin >> passwd; int score = 0; int len = passwd.size(); if (len <= 4) { score += 5; } else if (len >= 5 && len <= 7) { score += 10; } else { score += 25; } int up_num = find_c (passwd, isupper); int low_num = find_c (passwd, islower); int alpha_num = up_num + low_num; if (up_num > 0 && low_num > 0) { score += 20; } else if ((up_num > 0 && low_num == 0) || (up_num == 0 && low_num > 0)) { score += 10; } int dig_num = find_c(passwd, isdigit); if (dig_num == 1) { score += 10; } else if (dig_num > 1) { score += 20; } int sym_num = find_c(passwd, issymbol); if (sym_num == 1) { score += 10; } else if (sym_num > 1) { score += 25; } int bonus = 0; if (alpha_num >= 1 && dig_num >= 1) { bonus += 2; if (sym_num >= 1) { bonus += 1; if (up_num >= 1 && low_num >= 1) { bonus += 2; } } } score += bonus; 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 if (score >= 0) { cout << "VERY_WEAK" << endl; } return 0; }