题解 | 密码验证合格程序
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream>
#include <string>
#include <set>
using namespace std;
bool check(string pwd) {
if (pwd.length() < 8) { // 题目应是想说 长度不低于8
return false;
}
int spc[4] = {0, 0, 0, 0}; // 大写/小写/数字/特殊字符
for (auto elem : pwd) {
if (elem >= 'A' && elem <= 'Z')
spc[0] = 1;
else if (elem >= 'a' && elem <= 'z')
spc[1] = 1;
else if (elem >= '0' && elem <= '9')
spc[2] = 1;
else if (elem >= 33 && elem <= 126) { // 其他特殊字符
spc[3] = 1;
}
}
// 计算字符种类
int cnt = 0;
for (int i = 0; i <= 3; ++i) cnt += spc[i];
if (cnt < 3) {
return false;
}
// 判断是否存在长度>= 3的重复的独立字串
int len = pwd.length();
for (int i = 0; i <= len - 3; ++i) {
string s1 = pwd.substr(i, 3);
for (int j = i + 3; j <= len - 3; ++j) {
string s2(pwd.substr(j, 3));
if (s1 == s2) return false;
}
}
return true;
}
int main() {
string passwd;
while (cin >> passwd) {
if (check(passwd)) cout << "OK" << endl;
else cout << "NG" << endl;
}
return 0;
}
查看7道真题和解析