题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
while (cin >> str) {
// 1.
if (str.size() <= 8) {
cout << "NG" << endl;
continue;
}
// 2.
bool uppercase = false;
bool lowercase = false;
bool number = false;
bool other = false;
for (char ch : str) {
if (ch >= '0' && ch <= '9') {
number = true;
} else if (ch >= 'A' && ch <= 'Z') {
uppercase = true;
} else if (ch >= 'a' && ch <= 'z') {
lowercase = true;
} else {
other = true;
}
}
if (uppercase + lowercase + number + other < 3) {
cout << "NG" << endl;
continue;
}
// 3. 不能有长度超过2的重复,只需要考虑最小长度为3的子串是否有重复,利用find和rfind函数实现
bool condition3 = true;
for (int i = 0; i < str.size() - 2; ++i) {
string tmp = str.substr(i, 3);
if (str.find(tmp) != str.rfind(tmp)) {
cout << "NG" << endl;
condition3 = false;
break;
}
}
if (condition3) {
cout << "OK" << endl;
}
}
}
// 64 位输出请用 printf("%lld")

联想公司福利 1477人发布
查看25道真题和解析