题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include<iostream>
using namespace std;
string isValid(string password);
string condition_2(string password);
string isSubRepeat(string password);
int main() {
string password;
while (cin >> password) {
cout << isValid(password) << endl;
}
}
string isValid(string password) {
if (password.length() <= 8)
return "NG";
else
return condition_2(password);
}
string condition_2(string password) {
int condition[4] = { 0 };//condition[0]表示是否有数字,1大写字母,2小写字母,3其它字符
for (int i = 0; i < password.length(); i++) {
char ch = password[i];
if (isdigit(ch))
condition[0] = 1;
else if (ch >= 'A' && ch <= 'Z')
condition[1] = 1;
else if (ch >= 'a' && ch <= 'z')
condition[2] = 1;
else {
if (ch != ' ')
condition[3] = 1;
}
}
int sum = 0;
for (int i = 0; i < 4; i++)
sum += condition[i];
if (sum >= 3)
return isSubRepeat(password);
else
return "NG";
}
string isSubRepeat(string password) {
string sub1, sub2;
for (int i = 0; i < password.length() - 3; i++) {
sub1 = password.substr(i, 3);
for (int j = 0; j < password.length() - 3; j++) {
if (i != j) {
sub2 = password.substr(j, 3);
if (sub1 == sub2)
return "NG";
}
}
}
return "OK";
}