题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream>
#include <string>
using namespace std;
bool IsValid(const std::string& passport) {
if (passport.size() <= 8) {
return false;
}
int has_up_char = 0;
int has_low_char = 0;
int has_digit = 0;
int has_others_char = 0;
for (const char c : passport) {
if (c >= 'A' && c <= 'Z') {
has_up_char = 1;
} else if (c >= 'a' && c <= 'z') {
has_low_char = 1;
} else if (c >= '0' && c <= '9') {
has_digit = 1;
} else {
if (c != ' ' && c != '\n') {
has_others_char = 1;
}
}
}
int sum = has_digit + has_low_char + has_others_char + has_up_char;
if (sum < 3) {
return false;
}
for (int pos = 0; pos < passport.size() - 2; pos++) {
for (int len = 3; pos + len < passport.size(); len++) {
std::string str = passport.substr(pos, len);
if (passport.find(str, pos + len) != std::string::npos) {
return false;
}
}
}
return true;
}
int main() {
std::string passport;
while (getline(cin, passport)) {
if (IsValid(passport)) {
std::cout << "OK" << std::endl;
} else {
std::cout << "NG" << std::endl;
}
}
return 0;
}
// 64 位输出请用 printf("%lld")
正浩创新EcoFlow公司福利 660人发布
