在西西艾弗网上,用户的密码是一个由大写字母(A-Z)、小写字母(a-z)、数字(0-9)和特殊字符(* 和 #)共 64 种字符组成的字符串。根据复杂程度不同,密码安全度被分为高、中、低三档。
高:由上述 64 种字符组成,长度大于等于 6 个字符,包含字母、数字和特殊字符,同一个字符出现不超过 2次;
中:由上述 64 种字符组成,长度大于等于 6 个字符,包含字母、数字和特殊字符,且未达到高安全度要求;
低:由上述 64 种字符组成,长度大于等于 6 个字符,且未达到中安全度要求;
小 P 为自己准备了 n 个候选密码,试编写程序帮小 P 自动判别每个密码的安全级别。保证这 n 个密码都至少满足低安全度要求,当安全度为高、中、低时分别输出 2、1、0 即可。
输入格式:
从标准输入读入数据。
输入共 n+1 行。
第一行包含一个正整数 n,表示待判别的密码个数;
接下来 n 行,每行一个字符串,表示一个安全度至少为低的候选密码。
输出格式:
输出到标准输出。
输出共 n 行,每行输出一个整数 2、1 或 0,表示对应密码的安全度。
样例输入:
4
csp#ccsp
csp#ccsp2024
Csp#ccsp2024
CSP#2024
样例输出:
0
1
2
2
样例解释:
第一个密码不含数字,安全度为低;
第二个密码中小写字母 c 出现 3 次,安全度为中;
和第二个密码相比,第三个密码把一个小写字母 c 变为了大写,满足了高安全度要求;第四个密码同样满足高安全度要求。
子任务:
全部的测试数据满足n≤100,且输入的每个字符串均不超过 20 个字符。
C++
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
// 函数用于判断密码是否满足高安全度要求
bool isHighSecurity(const string& password) {
unordered_map<char, int> charCount;
bool hasLetter = false, hasNumber = false, hasSpecial = false;
for (char ch : password) {
// 统计字符出现次数
charCount[ch]++;
// 检查是否包含字母、数字和特殊字符
if (isalpha(ch)) hasLetter = true;
else if (isdigit(ch)) hasNumber = true;
else hasSpecial = true;
// 检查是否有字符出现超过2次
if (charCount[ch] > 2) return false;
}
// 检查密码长度是否大于等于6
if (password.length() < 6) return false;
// 检查是否包含字母、数字和特殊字符
return hasLetter && hasNumber && hasSpecial;
}
// 函数用于判断密码是否满足中安全度要求
bool isMediumSecurity(const string& password) {
bool hasLetter = false, hasNumber = false, hasSpecial = false;
for (char ch : password) {
// 检查是否包含字母、数字和特殊字符
if (isalpha(ch)) hasLetter = true;
else if (isdigit(ch)) hasNumber = true;
else hasSpecial = true;
}
// 检查密码长度是否大于等于6
if (password.length() < 6) return false;
// 检查是否包含字母、数字和特殊字符
return hasLetter && hasNumber && hasSpecial;
}
int main() {
int n;
cin >> n; // 读取密码个数
for (int i = 0; i < n; ++i) {
string password;
cin >> password; // 读取密码
if (isHighSecurity(password)) {
cout << 2 << endl; // 高安全度
} else if (isMediumSecurity(password)) {
cout << 1 << endl; // 中安全度
} else {
cout << 0 << endl; // 低安全度
}
}
return 0;
}
C语言
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
// 判断密码是否满足高安全度要求
bool isHighSecurity(const char* password) {
int char_count[256] = {0};
bool has_letter = false, has_number = false, has_special = false;
int length = strlen(password);
if (length < 6) {
return false;
}
for (int i = 0; i < length; ++i) {
unsigned char ch = password[i];
char_count[ch]++;
if (char_count[ch] > 2) {
return false;
}
if (isalpha(ch)) {
has_letter = true;
} else if (isdigit(ch)) {
has_number = true;
} else {
has_special = true;
}
}
return has_letter && has_number && has_special;
}
// 判断密码是否满足中安全度要求
bool isMediumSecurity(const char* password) {
bool has_letter = false, has_number = false, has_special = false;
int length = strlen(password);
if (length < 6) {
return false;
}
for (int i = 0; i < length; ++i) {
unsigned char ch = password[i];
if (isalpha(ch)) {
has_letter = true;
} else if (isdigit(ch)) {
has_number = true;
} else {
has_special = true;
}
}
return has_letter && has_number && has_special;
}
int main() {
int n;
scanf("%d", &n);
char password[21]; // 密码最长为20个字符
for (int i = 0; i < n; ++i) {
scanf("%s", password);
if (isHighSecurity(password)) {
printf("2\n");
} else if (isMediumSecurity(password)) {
printf("1\n");
} else {
printf("0\n");
}
}
return 0;
}