2025华为OD机试题库(按算法分类):2025华为OD统一考试题库清单(持续收录中)以及考点说明(Python/JS/C/C++)。
专栏导读
本专栏收录于《华为OD机试真题(Python/JS/C/C++)》。
刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。
一、题目描述
密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分。
1、密码长度:
5 分: 小于等于4 个字符
10 分: 5 到7 字符
25 分: 大于等于8 个字符
2、字母:
0 分: 没有字母
10 分: 密码里的字母全都是小(大)写字母
20 分: 密码里的字母符合”大小写混合“
3、数字:
0 分: 没有数字
10 分: 1 个数字
20 分: 大于1 个数字
4、符号:
0 分: 没有符号
10 分: 1 个符号
25 分: 大于1 个符号
5、奖励(只能选符合最多的那一种奖励):
2 分: 字母和数字
3 分: 字母、数字和符号
5 分: 大小写字母、数字和符号
6、最后的评分标准:
= 90: 非常安全
= 80: 安全(Secure)
= 70: 非常强
= 60: 强(Strong)
= 50: 一般(Average)
= 25: 弱(Weak)
= 0: 非常弱(Very_Weak)
7、对应输出为:
VERY_SECURE
SECURE
VERY_STRONG
STRONG
AVERAGE
WEAK
VERY_WEAK
请根据输入的密码字符串,进行安全评定。
8、注意:
字母:a-z, A-Z
数字:0-9
符号包含如下: (ASCII码表可以在UltraEdit的菜单view->ASCII Table查看)
!"#$%&'()*+,-./ (ASCII码:0x21~0x2F)
:;<=>?@ (ASCII码:0x3A~0x40)
[]^_` (ASCII码:0x5B~0x60)
{|}~ (ASCII码:0x7B~0x7E)
9、提示:
1 <= 字符串的长度<= 300
二、输入描述
输入一个string的密码。
三、输出描述
输出密码等级。
四、测试用例
测试用例1
1、输入
123456
2、输出
WEAK
3、说明
长度6(5~7):10分
无字母:0分
数字:6个数字:20分
无符号:0分
奖励:不满足奖励条件
总分: 10 + 0 + 20 = 30,输出为 WEAK
测试用例2
1、输入
abCD12
2、输出
AVERAGE
3、说明
长度6(5~7):10分
字母:含大写与小写:20分
数字:2个数字:20分
无符号:0分
奖励:字母和数字:2分
总分: 10 + 20 + 20 + 2 = 52,输出为 AVERAGE
五、解题思路
- 输入一个密码字符串;
- 根据规则计算密码的得分;
- 统计密码的长度,根据长度分配得分;
- 遍历密码字符串,统计大小写字母、数字和符号的数量;
- 根据统计结果给各类字符分配得分;
- 根据得分计算密码的安全等级;
- 输出密码的安全等级。
六、Python算法源码
# -*- coding: utf-8 -*-
# Python版密码安全等级评定程序
def get_password_score(password):
# 初始化分数
score = 0
# 获取密码长度
length = len(password)
# 根据密码长度进行评分
if length <= 4:
score += 5
elif 5 <= length <= 7:
score += 10
else:
score += 25
# 初始化大写字母、小写字母、数字、符号的计数器
upCount = lowCount = numCount = sigCount = 0
# 遍历密码中每个字符,统计不同类型的字符数量
for ch in password:
if ch.isupper():
upCount += 1 # 大写字母计数器加1
elif ch.islower():
lowCount += 1 # 小写字母计数器加1
elif ch.isdigit():
numCount += 1 # 数字计数器加1
else:
sigCount += 1 # 符号计数器加1
# 根据字母情况进行评分
if (upCount > 0 and lowCount == 0) or (upCount == 0 and lowCount > 0):
score += 10
elif upCount > 0 and lowCount > 0:
score += 20
# 根据数字个数进行评分
if numCount == 1:
score += 10
elif numCount > 1:
score += 20
# 根据符号个数进行评分
if sigCount == 1:
score += 10
elif sigCount > 1:
score += 25
# 根据奖励规则进行评分(只选一个最高奖励)
if upCount > 0 and lowCount > 0 and numCount > 0 and sigCount > 0:
score += 5
elif numCount > 0 and sigCount > 0 and (upCount > 0 or lowCount > 0):
score += 3
elif numCount > 0 and (upCount > 0 or lowCount > 0):
score += 2
return score
def get_security_level(score):
# 根据总分返回对应的密码安全等级
if score >= 90:
return "VERY_SECURE"
elif score >= 80:
return "SECURE"
elif score >= 70:
return "VERY_STRONG"
elif score >= 60:
return "STRONG"
elif score >= 50:
return "AVERAGE"
elif score >= 25:
return "WEAK"
else:
return "VERY_WEAK"
def main():
import sys
# 从标准输入中逐行读取密码
for line in sys.stdin:
password = line.strip() # 去掉两端的空白字符
if not password:
continue # 如果为空则跳过
# 计算密码得分
score = get_password_score(password)
# 输出对应的密码安全等级
print(get_security_level(score))
if __name__ == "__main__":
main()
七、JavaScript算法源码
/*
JavaScript版密码安全等级评定程序
从标准输入逐行读取密码,输出对应的安全等级
*/
const readline = require('readline'); // 引入readline模块用于读取标准输入
// 计算密码得分的函数
function getPasswordScore(password) {
let score = 0;
const length = password.length;
// 根据密码长度进行评分
if (length <= 4) {
score += 5;
} else if (length >= 5 && length <= 7) {
score += 10;
} else {
score += 25;
}
// 初始化大写字母、小写字母、数字、符号的计数器
let upCount = 0, lowCount = 0, numCount = 0, sigCount = 0;
// 遍历密码中每个字符,统计不同类型字符的数量
for (let i = 0; i < length; i++) {
const ch = password.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
upCount++; // 大写字母计数器加1
} else if (ch >= 'a' && ch <= 'z') {
lowCount++; // 小写字母计数器加1
} else if (ch >= '0' && ch <= '9') {
numCount++; // 数字计数器加1
} else {
sigCount++; // 符号计数器加1
}
}
// 根据字母情况进行评分
if ((upCount > 0 && lowCount === 0) || (upCount === 0 && lowCount > 0)) {
score += 10;
} else if (upCount > 0 && lowCount > 0) {
score += 20;
}
// 根据数字个数进行评分
if (numCount === 1) {
score += 10;
} else if (numCount > 1) {
score += 20;
}
// 根据符号个数进行评分
if (sigCount === 1) {
score += 10;
} else if (sigCount > 1) {
score += 25;
}
// 根据奖励规则进行评分(只选一个最高奖励)
if (upCount > 0 && lowCount > 0 && numCount > 0 && sigCount > 0) {
score += 5;
} else if (numCount > 0 && sigCount > 0 && (upCount > 0 || lowCount > 0)) {
score += 3;
} else if (numCount > 0 && (upCount > 0 || lowCount > 0)) {
score += 2;
}
return score;
}
// 根据得分返回密码安全等级的函数
function getSecurityLevel(score) {
if (score >= 90) {
return "VERY_SECURE";
} else if (score >= 80) {
return "SECURE";
} else if (score >= 70) {
return "VERY_STRONG";
} else if (score >= 60) {
return "STRONG";
} else if (score >= 50) {
return "AVERAGE";
} else if (score >= 25) {
return "WEAK";
} else {
return "VERY_WEAK";
}
}
// 设置readline接口,读取标准输入
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
// 逐行读取输入并处理
rl.on('line', function(line){
const password = line.trim(); // 去除首尾空白字符
if(password.length === 0) return; // 如果为空则跳过
const score = getPasswordScore(password);
console.log(getSecurityLevel(score));
});
八、C算法源码
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// 计算密码得分的函数
int getPasswordScore(const char *password) {
int score = 0;
int len = strlen(password);
// 如果末尾有换行符,则不计入长度
if (len > 0 && password[len-1] == '\n') {
len--;
}
// 根据密码长度进行评分
if (len <= 4) {
score += 5;
} else if (len >= 5 && len <= 7) {
score += 10;
} else {
score += 25;
}
int upCount = 0, lowCount = 0, numCount = 0, sigCount = 0;
// 遍历密码中每个字符,统计大写字母、小写字母、数字和符号的数量
for (int i = 0; i < len; i++) {
char ch = password[i];
if (isupper(ch)) {
upCount++;
} else if (islower(ch)) {
lowCount++;
} else if (isdigit(ch)) {
numCount++;
} else {
sigCount++;
}
}
// 根据字母情况进行评分
if ((upCount > 0 && lowCount == 0) || (upCount == 0 && lowCount > 0)) {
score += 10;
} else if (upCount > 0 && lowCount > 0) {
score += 20;
}
// 根据数字个数进行评分
if (numCount == 1) {
score += 10;
} else if (numCount > 1) {
score += 20;
}
// 根据符号个数进行评分
if (sigCount == 1) {
score += 10;
} else if (sigCount > 1) {
score += 25;
}
// 根据奖励规则进行评分(只选最高的奖励)
if (upCount > 0 && lowCount > 0 && numCount > 0 && sigCount > 0) {
score += 5;
} else if (numCount > 0 && sigCount > 0 && (upCount > 0 || lowCount > 0)) {
score += 3;
} else if (numCount > 0 && (upCount > 0 || lowCount > 0)) {
score += 2;
}
return score;
}
// 根据得分返回密码安全等级
const char* getSecurityLevel(int score) {
if (score >= 90) {
return "VERY_SECURE";
} else if (score >= 80) {
return "SECURE";
} else if (score >= 70) {
return "VERY_STRONG";
} else if (score >= 60) {
return "STRONG";
} else if (score >= 50) {
return "AVERAGE";
} else if (score >= 25) {
return "WEAK";
} else {
return "VERY_WEAK";
}
}
int main() {
char password[310]; // 定义存放密码的字符数组(最多300字符,加上换行符和结束符)
// 使用fgets逐行读取输入
while (fgets(password, sizeof(password), stdin)) {
// 去除末尾的换行符
int len = strlen(password);
if (len > 0 && password[len-1] == '\n') {
password[len-1] = '\0';
}
if (strlen(password) == 0) continue; // 若为空则跳过
int score = getPasswordScore(password);
// 输出对应的密码安全等级
printf("%s\n", getSecurityLevel(score));
}
return 0;
}
九、C++算法源码
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
// 计算密码得分的函数
int getPasswordScore(const string &password) {
int score = 0;
int len = password.length();
// 根据密码长度进行评分
if (len <= 4) {
score += 5;
} else if (len >= 5 && len <= 7) {
score += 10;
} else {
score += 25;
}
int upCount = 0, lowCount = 0, numCount = 0, sigCount = 0;
// 遍历密码中每个字符,统计大写字母、小写字母、数字和符号的数量
for (char ch : password) {
if (isupper(ch)) {
upCount++;
} else if (islower(ch)) {
lowCount++;
} else if (isdigit(ch)) {
numCount++;
} else {
sigCount++;
}
}
// 根据字母情况进行评分
if ((upCount > 0 && lowCount == 0) || (upCount == 0 && lowCount > 0)) {
score += 10;
} else if (upCount > 0 && lowCount > 0) {
score += 20;
}
// 根据数字个数进行评分
if (numCount == 1) {
score += 10;
} else if (numCount > 1) {
score += 20;
}
// 根据符号个数进行评分
if (sigCount == 1) {
score += 10;
} else if (sigCount > 1) {
score += 25;
}
// 根据奖励规则进行评分(只选最高奖励)
if (upCount > 0 && lowCount > 0 && numCount > 0 && sigCount > 0) {
score += 5;
} else if (numCount > 0 && sigCount > 0 && (upCount > 0 || lowCount > 0)) {
score += 3;
} else if (numCount > 0 && (upCount > 0 || lowCount > 0)) {
score += 2;
}
return score;
}
// 根据得分返回对应的密码安全等级
string getSecurityLevel(int score) {
if (score >= 90) {
return "VERY_SECURE";
} else if (score >= 80) {
return "SECURE";
} else if (score >= 70) {
return "VERY_STRONG";
} else if (score >= 60) {
return "STRONG";
} else if (score >= 50) {
return "AVERAGE";
} else if (score >= 25) {
return "WEAK";
} else {
return "VERY_WEAK";
}
}
int main() {
string password;
// 使用getline逐行读取输入
while (getline(cin, password)) {
if (password.empty()) continue; // 若输入为空则跳过
int score = getPasswordScore(password);
// 输出对应的密码安全等级
cout << getSecurityLevel(score) << endl;
}
return 0;
}
🏆下一篇:华为OD机试真题 - 简易内存池(Python/JS/C/C++ 2024 E卷 200分)
🏆本文收录于,华为OD机试真题(Python/JS/C/C++)
刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。