题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
#include <bits/stdc++.h> #include <cctype> using namespace std; void CheckLength(std::string pwd, int &point) { auto len = pwd.size(); if (len <= 4) { point += 5; } else if (len >= 5 && len <= 7) { point += 10; } else if (len >= 8) { point += 25; } } void CheckAlpha(std::string pwd, int &point) { int count = 0; int hasLower = 0; int hasUpper = 0; for (const char ch : pwd) { if (isalpha(ch)) { if (isupper(ch)) { hasUpper = 1; } if (islower(ch)) { hasLower = 1; } } } int ret = hasLower + hasUpper; if (ret == 0) { point += 0; } else if (ret == 1) { point += 10; } else if (ret == 2) { point += 20; } } void CheckDigit(std::string pwd, int &point) { int count = 0; int didgitCount = 0; for (const char ch : pwd) { if (isdigit(ch)) { didgitCount++; } } if (didgitCount == 0) { point += 0; } else if (didgitCount == 1) { point += 10; } else { point += 20; } } void CheckSymbol(std::string pwd, int &point) { int count = 0; int symbolCount = 0; for (const char ch : pwd) { if (ch >= 0x21 && ch <= 0x2f || ch > 0x3A && ch <= 0X40 || ch > 0x5B && ch <= 0X60 || ch > 0x7B && ch <= 0X7E ) { symbolCount++; } } if (symbolCount == 0) { point += 0; } else if (symbolCount == 1) { point += 10; } else { point += 25; } } void CheckBonus(std::string pwd, int &point) { bool hasAlpha = false, hasLower = false, hasUpper = false, hasdigit = false, hasSymbol = false; for (const char ch : pwd) { if (isalpha(ch)) { hasAlpha = true; if (isupper(ch)) { hasUpper = true; } if (islower(ch)) { hasLower = true; } } if (isdigit(ch)) { hasdigit = true; } if ((ch >= 0x21 && ch <= 0x2F) || (ch >= 0x3A && ch <= 0X40) || (ch >= 0x5B && ch <= 0X60) || (ch >= 0x7B && ch <= 0X7E) ) { hasSymbol = true; } } if (hasUpper && hasLower && hasdigit && hasSymbol) { point += 5; return; } if (hasAlpha && hasdigit && hasSymbol) { point += 3; return; } if (hasAlpha && hasdigit) { point += 2; return; } } void PrintResult(int point) { string res; if (point >= 90) { res = "VERY_SECURE"; } else if (point >= 80 && point < 90) { res = "SECURE"; } else if (point >= 70 && point < 80) { res = "VERY_STRONG"; } else if (point >= 60 && point < 70) { res = "STRONG"; } else if (point >= 50 && point < 60) { res = "AVERAGE"; } else if (point >= 25 && point < 50) { res = "WEAK"; } else { res = "VERY_WEAK"; } cout << res << endl; } void foo(string& pwd, int point) { CheckLength(pwd, point); CheckAlpha(pwd, point); CheckDigit(pwd, point); CheckSymbol(pwd, point); CheckBonus(pwd, point); PrintResult(point); } int main() { int point = 0; string pwd; while (getline(cin, pwd)) { foo(pwd, point); } return 0; }