题解 | 密码验证合格程序
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream>
#include <string>
#include <cctype> // 用于 isalnum 函数
using namespace std;
// 大写字母检测函数
int Captal_detection(const string& s) {
for (char ch : s) {
if (ch >= 'A' && ch <= 'Z') {
return 1;
}
}
return 0;
}
// 小写字母检测函数
int Lower_detection(const string& s) {
for (char ch : s) {
if (ch >= 'a' && ch <= 'z') {
return 1;
}
}
return 0;
}
// 数字检测函数
int Digit_detection(const string& s) {
for (char ch : s) {
if (ch >= '0' && ch <= '9') {
return 1;
}
}
return 0;
}
// 特殊字符检测函数
int Special_detection(const string& s) {
for (char ch : s) {
if (!isalnum(ch)) { // 不是字母或数字即为特殊字符
return 1;
}
}
return 0;
}
// 密码合法性检查函数
bool Islegal(const string& s) {
if (s.size() <= 8) return false;
int detection = Captal_detection(s) + Lower_detection(s) + Digit_detection(s) + Special_detection(s);
if (detection < 3) return false;
//遍历寻找
for (size_t i = 0; i < s.length() - 5; ++i) {
for (size_t j = i + 3; j < s.length() - 2; ++j) {
if (s.substr(i, 3) == s.substr(j, 3)) {
return false;
}
}
}
return true;
}
int main() {
string s;
while (getline(cin, s)) {
if (Islegal(s))
cout << "OK" << endl;
else
cout << "NG" << endl;
}
return 0;
}
