题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
#include <bits/stdc++.h> using namespace std; vector<pair<int, string>> m = { {90, "VERY_SECURE"}, {80, "SECURE"}, {70, "VERY_STRONG"}, {60, "STRONG"}, {50, "AVERAGE"}, {25, "WEAK"}, {0, "VERY_WEAK"}, }; int codeLength(string s){ if(s.size() <= 4){ return 5; } else if(s.size() >= 5 && s.size() <= 7){ return 10; } else if(s.size() >= 8){ return 25; } return 0; } int isChar(string s){ bool haveChar = false; bool isDaXie = false; bool isXiaoXie = false; for(char ch : s){ if(isalpha(ch)){ haveChar = true; //有字母 if(isupper(ch)){ isDaXie = true; } else if(islower(ch)){ isXiaoXie = true; } } } if(!haveChar) return 0; else if((isDaXie && !isXiaoXie) || (!isDaXie && isXiaoXie)) return 10; else if(isDaXie && isXiaoXie) return 20; return 0; } int isDigit(string s){ int tmp = 0; for(char ch : s){ if(isdigit(ch)){ tmp++; } } if(tmp == 0) return 0; else if(tmp == 1) return 10; else if(tmp > 1) return 20; return 0; } //判断符号使用: //ch>=0x21&&ch<=0x2F || ch>=0x3A&&ch<=0x40 || ch>=0x5B&&ch<=0x60 || ch>=0x7B&&ch<=0x7E; int symbol(string s){ string str = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"; int tmp = 0; for(char ch : s){ if(/*strstr(str.c_str(), &ch)*/ ch>=0x21&&ch<=0x2F || ch>=0x3A&&ch<=0x40 || ch>=0x5B&&ch<=0x60 || ch>=0x7B&&ch<=0x7E ){ //strstr(str.c_str(), &ch); str中查找ch tmp++; } } if(tmp == 0) return 0; else if(tmp == 1) return 10; else if(tmp > 1) return 25; return 0; } int reward(string s){ string str = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"; bool ziMu = false; bool shuZi = false; bool daXie = false; bool xiaoXie = false; bool fuHao = false; for(char ch : s){ if(isalpha(ch)){ ziMu = true; if(isupper(ch)){ daXie = true; } else if(islower(ch)){ xiaoXie = true; } } if(isdigit(ch)){ shuZi = true; } if(/*str.find(ch)*/ ch>=0x21&&ch<=0x2F || ch>=0x3A&&ch<=0x40 || ch>=0x5B&&ch<=0x60 || ch>=0x7B&&ch<=0x7E ){ fuHao = true; } } if(ziMu && shuZi && daXie && xiaoXie && fuHao) return 5; else if(ziMu && shuZi && fuHao) return 3; else if(ziMu && shuZi) return 2; return 0; } int main(){ string s = ""; while(cin >> s){ int res = 0; res += codeLength(s); //cout << "res1 = " << res << endl; res += isChar(s); //cout << "res2 = " << res << endl; res += isDigit(s); //cout << "res3 = " << res << endl; res += symbol(s); //cout << "res4 = " << res << endl; res += reward(s); //cout << "res = " << res << endl; for(auto iter = m.begin(); iter != m.end(); iter++){ //cout << iter->first << endl; if(iter->first <= res){ cout << iter->second << endl; break; } } } return 0; }
华为题库题解 文章被收录于专栏
牛客华为题库的题解