题目链接
题目描述
给定 个密码,需要判断每个密码是否合法。合法的密码需要同时满足以下所有条件:
- 密码只能由大写字母、小写字母、数字构成(输入保证满足此条)。
- 密码不能以数字开头。
- 密码中至少出现大写字母、小写字母和数字这三种字符类型中的两种。
- 密码长度至少为 8。
解题思路
这是一个直接的模拟题,我们只需要为每个输入的密码字符串编写一个函数,逐一检查它是否满足所有四个条件。只要有一个条件不满足,该密码就是不合法的。
算法流程:
我们可以设计一个 isValid(password)
函数,按顺序执行以下检查:
-
检查长度:
判断
password.length()
是否小于 8。如果是,则不合法,直接返回false
。 -
检查首字符:
判断
password[0]
是否为数字。如果是,则不合法,直接返回false
。 -
检查字符种类:
- 我们需要统计密码中出现了哪些类型的字符。可以设置三个布尔标志位:
hasUpper
,hasLower
,hasDigit
,初始值都为false
。 - 遍历整个密码字符串。
- 对于字符串中的每一个字符
c
:- 如果
c
是大写字母,将hasUpper
设为true
。 - 如果
c
是小写字母,将hasLower
设为true
。 - 如果
c
是数字,将hasDigit
设为true
。
- 如果
- 遍历结束后,计算种类数
types = (int)hasUpper + (int)hasLower + (int)hasDigit
。 - 如果
types < 2
,则不合法,返回false
。
- 我们需要统计密码中出现了哪些类型的字符。可以设置三个布尔标志位:
-
最终结论:
如果一个密码通过了以上所有检查,那么它就是合法的,函数返回
true
。
主程序中,我们循环 次,对每个读入的密码调用此函数,并根据返回值输出 "YES" 或 "NO"。
代码
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
bool is_valid(const string& password) {
// 4. 检查长度
if (password.length() < 8) {
return false;
}
// 2. 检查首字符
if (isdigit(password[0])) {
return false;
}
// 3. 检查字符种类
bool has_upper = false;
bool has_lower = false;
bool has_digit = false;
for (char c : password) {
if (isupper(c)) {
has_upper = true;
} else if (islower(c)) {
has_lower = true;
} else if (isdigit(c)) {
has_digit = true;
}
}
int types = 0;
if (has_upper) types++;
if (has_lower) types++;
if (has_digit) types++;
if (types < 2) {
return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
string password;
while (n--) {
cin >> password;
if (is_valid(password)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine(); // consume the newline
for (int i = 0; i < n; i++) {
String password = sc.nextLine();
if (isValid(password)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
public static boolean isValid(String password) {
// 4. 检查长度
if (password.length() < 8) {
return false;
}
// 2. 检查首字符
if (Character.isDigit(password.charAt(0))) {
return false;
}
// 3. 检查字符种类
boolean hasUpper = false;
boolean hasLower = false;
boolean hasDigit = false;
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
hasUpper = true;
} else if (Character.isLowerCase(c)) {
hasLower = true;
} else if (Character.isDigit(c)) {
hasDigit = true;
}
}
int types = 0;
if (hasUpper) types++;
if (hasLower) types++;
if (hasDigit) types++;
return types >= 2;
}
}
import sys
def is_valid(password):
# 4. 检查长度
if len(password) < 8:
return False
# 2. 检查首字符
if password[0].isdigit():
return False
# 3. 检查字符种类
has_upper = False
has_lower = False
has_digit = False
for char in password:
if char.isupper():
has_upper = True
elif char.islower():
has_lower = True
elif char.isdigit():
has_digit = True
types = sum([has_upper, has_lower, has_digit])
if types < 2:
return False
return True
def solve():
try:
n_str = sys.stdin.readline()
if not n_str: return
n = int(n_str)
for _ in range(n):
password = sys.stdin.readline().strip()
if is_valid(password):
print("YES")
else:
print("NO")
except (IOError, ValueError):
return
solve()
算法及复杂度
-
算法:模拟、字符串处理
-
时间复杂度:
,其中
是密码的数量,
是密码的最大长度。我们需要对每个密码都完整遍历一遍来检查字符种类。
-
空间复杂度:
,主要用于存储读入的密码字符串。检查过程本身只使用常数级别的额外空间。