题解 | 密码验证合格程序
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream>
using namespace std;
bool isStyle(string s){
bool hasCapitalLetter = false;
bool hasSmallLetter = false;
bool hasDigital = false;
bool hasSpecialCharacter = false;
int count = 0;
for(const auto &c : s){
if(c >= 'A' && c <= 'Z') {
if(!hasCapitalLetter){
count ++ ;
}
hasCapitalLetter = true;
}
if(c >= 'a' && c <= 'z'){
if(!hasSmallLetter){
count ++ ;
}
hasSmallLetter = true;
}
if(c >= '0' && c <= '9'){
if(!hasDigital){
count ++ ;
}
hasDigital = true;
}
if(c < '0' || c > '9' && c < 'A' || c > 'Z' && c < 'a' || c > 'z'){
if(!hasSpecialCharacter){
count ++ ;
}
hasSpecialCharacter = true;
}
}
return count >= 3;
}
/**
* 暴力
* 如果存在两个长度大于2的独立子串返回true
*/
bool isSubStr(string s){
for(int i = 0; i < s.size() - 2; i += 1){
for(int j = i + 3; j < s.size() - 2; j += 1){
// cout << s[i] << s[i + 1] << " " << s[j] << s[j + 1] << "\n";
if(s[i] == s[j] && s[i + 1] == s[j + 1] && s[i + 2] == s[j + 2]){
return true;
}
}
}
return false;
}
int main() {
string s;
while(cin >> s){
if(s.size() <= 8){
cout << "NG\n";
continue;
}
if(!isStyle(s)){
cout << "NG\n";
continue;
}
if(isSubStr(s)){
cout << "NG\n";
continue;
}
cout << "OK\n";
}
return 0;
}
// 64 位输出请用 printf("%lld")
OPPO公司福利 1133人发布