题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool elemTypes(string s) {
vector<int> t(4, 0);
for (char c: s) {
if (c >= 'a' && c <= 'z') {
t[0] = 1;
} else if (c >= 'A' && c <= 'Z') {
t[1] = 1;
} else if (c >= '0' && c <= '9') {
t[2] = 1;
} else {
t[3] = 1;
}
}
return (t[0] + t[1] + t[2] + t[3] >= 3);
}
bool elemRep(string s) {
for (int i = 0; i < s.size() - 2; i++) {
for (int j = i + 3; j < s.size() - 2; j++) {
if (s[j] == s[i] && s[j + 1] == s[i + 1] && s[j + 2] == s[i + 2]) {
return false;
}
}
}
return true;
}
int main() {
string s;
while (getline(cin, s)) { // 注意 while 处理多个 case
// length
if (s.size() < 8) {
cout << "NG" << endl;
continue;
}
// 3 types
if (!elemTypes(s)) {
cout << "NG" << endl;
continue;
}
// repeatation
if (!elemRep(s)) {
cout << "NG" << endl;
continue;
}
cout << "OK" << endl;
}
}
// 64 位输出请用 printf("%lld")
查看2道真题和解析
凡岛公司福利 595人发布
