华为笔试 华为笔试题 0919
笔试时间:2024年09月19日 秋招
历史笔试传送门:2023秋招笔试合集
第一题
题目:网络健康检查
一线工程师每到重要节日需要对网络进行健康检查,在网络中对各个网元采集数据,判断当前网络是否健康。因每个网元的判断条件以及采集的数据不同,现在需要你对网络采集到的数据,以及工程师提供的判断条件进行解析。判断条件为布尔表达式,保证合法,字段名不会与关键字冲突。若采集数据符合条件,则认为网络健康,否则网络处于不健康状态。
输入描述
第一行有2个整数n, m,接下来有n行字符串,接下来m行,每行均有两个字符串key和value。
备注:表达式中仅会出现AND、OR、()、'、空格、=、字段名、数据(单引号内),给出的表达式一定是有效的,AND优先级高于OR,"="左侧为字段名,"="右侧为数据,类型为字符串。0<n≤5 0<m≤10。
输出描述
返回表达式结果:0,1。0表示健康,1表示不健康。
样例输入一
2 2
error = '0' AND (name = 'NE40' OR name = 'NE20')
error = '1' AND (name = 'NE40' OR name = 'NE20')
name NE40
error 0
样例输出一
0
1
样例输入二
3 2
error = '1' AND (name = 'NE40' OR name ='NE20')
error = '2' AND (name = 'NE40' OR name ='NE20')
error = '3' AND (name = 'NE40' OR name ='NE20')
name NE40
error 3
样例输出二
1
1
0
参考题解
模拟。对于回答的字符串,将里面的And 换成and , = 换成 == , OR 换成or。
C++:[此代码未进行大量数据的测试,仅供参考]
#include <iostream> #include <vector> #include <unordered_map> #include <string> #include <sstream> using namespace std; bool evaluateExpression(const string& expression, const unordered_map<string, bool>& replacements) { string modified = expression; size_t pos; // Replace "=" with "==" while ((pos = modified.find('=')) != string::npos) { modified.replace(pos, 1, "=="); } // Replace "AND" with "&&" and "OR" with "||" while ((pos = modified.find("AND")) != string::npos) { modified.replace(pos, 3, "&&"); } while ((pos = modified.find("OR")) != string::npos) { modified.replace(pos, 2, "||"); } // Evaluate the expression for (const auto& rep : replacements) { size_t pos = 0; while ((pos = modified.find(rep.first, pos)) != string::npos) { modified.replace(pos, rep.first.length(), rep.second ? "true" : "false"); pos += 4; // Move past the "true" or "false" } } return eval(modified); // You need a custom eval function or library for this } int main() { int num_expressions, num_replacements; cin >> num_expressions >> num_replacements; cin.ignore(); // Clear newline after numbers vector<string> expressions(num_expressions); for (int i = 0; i < num_expressions; ++i) { getline(cin, expressions[i]); } unordered_map<string, bool> replacements; for (int i = 0; i < num_replacements; ++i) { string key; bool value; cin >> key >> value; replacements[key] = value; } for (const auto& expression : expressions) { bool result = evaluateExpression(expression, replacements); cout << (result ? 0 : 1) << endl; } return 0; }
Java:[此代码未进行大量数据的测试,仅供参考]
import java.util.*; public class ExpressionEvaluator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int numExpressions = scanner.nextInt(); int numReplacements = scanner.nextInt(); scanner.nextLine(); // Clear newline after numbers List<String> expressions = new ArrayList<>(); for (int i = 0; i < numExpressions; i++) { expressions.add(scanner.nextLine()); } Map<String, Boolean> replacements = new HashMap<>(); for (int i = 0; i < numReplacements; i++) { String key = scanner.next(); boolean value = scanner.nextBoolean(); replacements.put(key, value); } for (String expression : expressions) { String modified = expression.replace("=", "==") .replace("AND", "&&") .replace("OR", "||"); for (Map.Entry<String, Boolean> entry : replacements.entrySet()) { modified = modified.replace(entry.getKey(), entry.getValue() ? "true" : "false"); } boolean result = eval(modified); // You need a custom eval method or library for this System.out.println(result ? 0 : 1); } scanner.close(); } // You will need to implement the eval method or use a library that supports boolean expression evaluation private static boolean eval(String expression) { // Implementation of eval or usage of a library goes here return false; // Placeholder return } }
Python:[此代码未进行大量数据的测试,仅供参考]
num_expressions, num_replacements = list(map(int, input().split())) expressions = [] for _ in range(num_expressions): expressions.append(input()) replacements = {} for _ in range(num_replacements): key, value = input().split() replacements[key] = str(value) for expression in expressions: expression = expression.replace("=", "==") expression = expression.replace("AND", "and") expression = expression.replace("OR", "or") result = eval(expression, globals(), replacements) print(0 if result else 1)
第二题
题目:防护设备
有一个N x N大小的迷宫。初始状态下,配送员位于迷宫的左上角,他希望前往迷宫的右下角。配送员只能沿着上下左右四个方向移动,从每个格子移动到相邻格子所需要的时间是1个单位,他必须用最多K个(也可以少于K个)单位时间到达右下角格子。迷宫的每个格子都有辐射值,配送员必须穿着防护能力不低于相应辐射值的防护服,才能通过该格子。他希望知道,防护服的防护能力最少要达到多少,他才能顺利完成任务。注意:配送员需要通过迷宫的左上角和右下角,因此防护服的防护能力必须大于等于这两个格子的辐射值。
输入描述
前两行各包含一个正整数,分别对应N和K。
后N行各包含N整数,以空格分隔,表示地图上每个位置的辐射值
输出描述
一个整数,表示配送员穿着防护服的最低防护能力。
样例输入一
2
2
1 3
2 1
样例输出一
2
说明:配送员可以选择通过左下角(辐射值为2)的路线,耗费2单位时间。
样例输入二
5
1 2
0 0 0 0 0
9 9 3 9 0
0 0 0 0 0
0 9 5 9 9
0 0 0 0 0
样例输出二
3
说明:最优路线:往右2格,往下2格,往左2格,往下2格,往右4格,耗费12单位时间,经过格子的最大辐射值为3。另外,在地图不变的情况下,如果K=16,输出为0;如果K=8,输出为5。
参考题解
首先确定防护力的最小和最大可能值。使用二分查找在这个范围内寻找最小的防护力。对于每一个中间值,利用广度优先搜索(BFS)从左上角出发,只有通过辐射值不超过当前中值的格子,并计算移动步数是否不超过K。如果可以到达终点,则尝试更小的防护力;否则,增加防护力。最终找到满足条件的最小防护力值。
C++:[此代码未进行大量数据的测试,仅供参考]
#include <bits/stdc++.h> using namespace std; int n, k; int a[100][100]; // BFS函数,检查是否可以在k步内到达终点,且所有经过的格子辐射值 ≤ val bool bfs(int val) { // 检查起点和终点的辐射值是否符合 if (a[0][0] > val || a[n-1][n-1] > val) return false; // 定义移动方向:右、左、下、上 int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; // 距离数组,记录每个格子的最短步数 vector<vector<int>> dist(n, vector<int>(n, -1)); dist[0][0] = 0; // 使用队列进行BFS,存储x, y坐标 queue<pair<int, int>> q; q.push({0, 0}); while (!q.empty()) { pair<int, int> current = q.front(); q.pop(); int x = current.first; int y = current.second; int steps = dist[x][y]; // 如果到达终点,检查步数是否 ≤ K if (x == n-1 && y == n-1) { return steps <= k; } // 遍历四个方向 for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; // 检查边界 if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue; // 检查辐射值和是否已访问 if (a[nx][ny] > val || dist[nx][ny] != -1) continue; // 检查步数是否超过K if (steps + 1 > k) continue; // 更新步数并加入队列 dist[nx][ny] = steps + 1; q.push({nx, ny}); } } // 如果无法到达终点 return false; } int main(){ ios::sync_with_stdio(false); cin.tie(0); //
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
持续收录字节、腾讯、阿里、美团、美团、拼多多、华为等笔试题解,包含python、C++、Java多种语言版本,持续更新中。