题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream>
#include<string>
using namespace std;
bool length(string& str) {
if (str.size() <= 8) {
cout << "NG" << endl;
return false;
}
return true;;
}
bool substring(string& str) {
for (int i = 0; i < str.size() - 3; i++) {
for (int j = i + 1; j < str.size() - 3; j++) {
if (str.substr(i, 3) == str.substr(j, 3)) {
cout << "NG" << endl;
return false;
break;
}
}
}
return true;
}
bool code_tppe(string& str) {
int type[4] = {0};
for (int i = 0; i < str.size(); i++) {
if (str[i] >= 65 && str[i] <= 90) { //判断是否为大写字符
type[0] = 1;
} else if (str[i] >= 97 && str[i] <= 122) { //判断是否为小写字符
type[1] = 1;
} else if (str[i] >= '0' && str[i] <= '9') {
type[2] = 1;
} else {
type[3] = 1;
}
}
int count = 0;
for (int i = 0; i < 4; i++) {
count += type[i];
}
if (count >= 3) {
return true;
} else {
cout << "NG" << endl;
return false;
}
}
int main() {
string str;
while (cin >> str) {
if (!length(str)) {
continue;
}
if (!substring(str)) {
continue;
}
if (!code_tppe(str)) {
continue;
}
cout << "OK" << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
查看29道真题和解析