酷代码 AI
菜单

题目描述 网站注册需要有用户名和密码,编写程序以检查用户输入密码的有效性。合规的密码 应满足以下要求 :。 只能由 a∼z 之间 26 个小写字母、A∼Z 之间 26 个大写字母、0∼9 之间 10 个数字 以及 !@#$ 四个特殊字符构成。 密码最短长度 :6 个字符,密码最大长度 :12 个字符。 大写字母,小写字母和数字必须至少有其中两种,以及至少有四个特殊字符中的一 个。 输入格式 输入一行不含空格的字符串。约定长度不超过 100。该字符串被英文逗号分隔为多 段,作为多组被检测密码。 输出格式 输出若干行,每行输出一组合规的密码。输出顺序以输入先后为序,即先输入则先输 出。 输入输出样例 输入 #1 seHJ12!@,sjdkffH$123,sdf!@&12HDHa!,123&^YUhg@! 输出 #1 seHJ12!@ sjdkffH$123 说明/提示 【样例 1 解释】 输入被英文逗号分为了四组被检测密码:seHJ12!@、sjdkffH$123、sdf!@&12HDHa!、 123&^YUhg@!。其中 sdf!@&12HDHa! 长度

```cpp #include <iostream> #include <string> #include <sstream> using namespace std; // 检查密码是否合规 bool isValidPassword(const string& password) { // 检查密码长度 if (password.length() < 6 || password.length() > 12) { return false; } bool hasLower = false; bool hasUpper = false; bool hasDigit = false; bool hasSpecial = false; // 遍历密码中的每个字符 for (char c : password) { if (c >= 'a' && c <= 'z') { hasLower = true; } else if (c >= 'A' && c <= 'Z') { hasUpper = true; } else if (c >= '0' && c <= '9') { hasDigit = true; } else if (c == '!' || c == '@' || c == '#' || c == '$') { hasSpecial = true; } else { // 包含非法字符 return false; } } // 检查是否至少包含两种字符类型和一个特殊字符 int typeCount = (hasLower? 1 : 0) + (hasUpper? 1 : 0) + (hasDigit? 1 : 0); return typeCount >= 2 && hasSpecial; } int main() { string input; getline(cin, input); // 使用字符串流分割输入 stringstream ss(input); string password; while (getline(ss, password, ',')) { if (isValidPassword(password)) { cout << password << endl; } } return 0; } ``` 代码解释: 1. `isValidPassword` 函数: - 首先检查密码长度是否在 6 到 12 个字符之间。 - 遍历密码中的每个字符,标记是否包含小写字母、大写字母、数字和特殊字符。 - 若包含非法字符,直接返回 `false`。 - 最后检查是否至少包含两种字符类型和一个特殊字符。 2. `main` 函数: - 读取一行输入。 - 使用 `stringstream` 按逗号分割输入字符串。 - 对每个分割后的密码调用 `isValidPassword` 函数检查是否合规,若合规则输出。 [2025-11-26 20:44:53 | AI问答 | 567点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多