最新华为OD机试真题-密码输入检测(100分)
🍭 大家好这里是清隆学长 ,一枚热爱算法的程序员
✨ 本系列打算持续跟新 华为OD机试-D卷 的三语言AC题解
👏 感谢大家的订阅➕ 和 喜欢💗
📎在线评测链接
🌍 评测功能需要 =>订阅专栏<= 后联系清隆解锁~
🍓OJ题目截图
密码输入检测
问题描述
K小姐最近在开发一个密码安全检测系统。系统会接收用户输入的密码字符串 ,其中字符
<
表示退格,可以清除前一个输入的字符。请你编写一个程序,处理用户的输入,输出最终的密码字符串,并判断该密码是否满足以下密码安全要求:
- 密码长度不小于
;
- 密码至少包含
个大写字母;
- 密码至少包含
个小写字母;
- 密码至少包含
个数字;
- 密码至少包含
个非字母、数字的特殊字符。
注意,空串退格后仍为空串,且用户输入的字符串不包含 <
字符和空白字符。
输入格式
输入一行字符串,表示用户输入的密码字符串 ,其中字符
<
表示退格操作。用户输入的字符串不包含空白字符。
输出格式
输出处理后的实际密码字符串,并用 ,
分隔输出该密码是否满足密码安全要求,若满足输出 true
,否则输出 false
。
样例输入
ABC<c89%000<
样例输出
ABc89%00,true
数据范围
字符串长度 。
题解
本题需要模拟用户输入密码的过程,处理退格操作,得到最终的密码字符串,并判断该密码是否满足题目给定的密码安全要求。
首先,我们可以遍历输入的字符串,遇到字符 <
时进行退格操作,即删除当前密码字符串的最后一个字符。遍历完整个字符串后,我们就得到了最终的密码字符串。
接下来,我们需要判断密码是否满足安全要求。根据题目描述,我们需要统计密码中大写字母、小写字母、数字以及特殊字符的个数。可以再次遍历最终的密码字符串,对每个字符进行判断,更新相应的计数器。
最后,根据密码长度以及各类字符的数量,判断是否满足题目给定的所有安全要求,输出相应的结果即可。
参考代码
- Python
def check(s):
n = len(s)
if n < 8:
return False
upper_cnt = lower_cnt = digit_cnt = 0
for ch in s:
if ch.islower():
lower_cnt += 1
elif ch.isupper():
upper_cnt += 1
elif ch.isdigit():
digit_cnt += 1
return upper_cnt >= 1 and lower_cnt >= 1 and digit_cnt >= 1 and (upper_cnt + lower_cnt + digit_cnt) < n
def process_input(s):
res = ''
for ch in s:
if ch == '<':
res = res[:-1]
else:
res += ch
is_valid = 'true' if check(res) else 'false'
print(f"{res},{is_valid}")
s = input()
process_input(s)
- Java
import java.util.Scanner;
public class Main {
public static boolean check(String s) {
int n = s.length();
if (n < 8) {
return false;
}
int upperCnt = 0, lowerCnt = 0, digitCnt = 0;
for (char ch : s.toCharArray()) {
if (Character.isLowerCase(ch)) {
lowerCnt++;
} else if (Character.isUpperCase(ch)) {
upperCnt++;
} else if (Character.isDigit(ch)) {
digitCnt++;
}
}
return upperCnt >= 1 && lowerCnt >= 1 && digitCnt >= 1 && (upperCnt + lowerCnt + digitCnt) < n;
}
public static void processInput(String s) {
StringBuilder res = new StringBuilder();
for (char ch : s.toCharArray()) {
if (ch == '<') {
if (res.length() > 0) {
res.deleteCharAt(res.length() - 1);
}
} else {
res.append(ch);
}
}
boolean isValid = check(res.toString());
System.out.println(res + "," + isValid);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
processInput(s);
}
}
- Cpp
#include <iostream>
#include <string>
using namespace std;
bool check(string s) {
int n = s.size();
if (n < 8) {
return false;
}
int upper_cnt = 0, lower_cnt = 0, digit_cnt = 0;
for (char ch : s) {
if (islower(ch)) {
lower_cnt++;
} else if (isupper(ch)) {
upper_cnt++;
} else if (isdigit(ch)) {
digit_cnt++;
}
}
return upper_cnt >= 1 && lower_cnt >= 1 && digit_cnt >= 1 && (upper_cnt + lower_cnt + digit_cnt) < n;
}
void processInput(string s) {
string res;
for (char ch : s) {
if (ch == '<') {
if (!res.empty()) {
res.pop_back();
}
} else {
res.push_back(ch);
}
}
bool is_valid = check(res);
cout << res << "," << boolalpha << is_valid << endl;
}
int main() {
string s;
getline(cin, s);
processInput(s);
return 0;
}
#23届找工作求助阵地##机械人怎么评价今年的华为##华为##华为OD##笔试#最新华为OD机试-D卷 文章被收录于专栏
本专栏给大家提供了华为2024最新华为OD-C/D卷的题目汇总和(Java/Cpp/Python)三语言解析 + 提供OJ在线评测