2023 华为笔试 华为笔试题 0920 您所在的位置:网站首页 华为笔试后 2023 华为笔试 华为笔试题 0920

2023 华为笔试 华为笔试题 0920

2024-04-03 23:15| 来源: 网络整理| 查看: 265

2023 华为笔试 华为笔试题 0920

笔试时间:2023年9月20日 秋招

第一题 题目:丢失报文的位置

某通信系统持续向外发送报文,使用数组nums保存n个最近发送的报文,用于在报文未达到对端的情况下重发。报文使用序号sn表示,序号sn按照报文发送顺序从小到大排序,相邻报文sn不完全连续且有可能相同。报文使用循环覆盖的方式保存,即nums数组填满后,从头开始保存新的报文。假设需要重发序号为sn的报文。请找出序号为sn的报文在数组中的开始位置和结束位置。

解答要求:时间限制:C/C++1000ms,其他语言: 2000ms内存限制: C/C++256MB其他语言:512MB

输入描述

第一行输入:数组nums的大小n,取值范围[0,10000]

第二行输入:数组中的所有报文的序号sn,sn取值范围[0,100000]。

第三行输入:需要重发的报文序号sn,取值范围[0,100000]

输出描述

start end

说明:start和end代表需要重发的报文序号sn在数组中的起始下标和结束下标

样例输入

7

0 0 1 2 2 5 6

1

样例输出

2 2

解释

nums数组大小为7。保存了7个报文,sn分别是0 0 1 2 2 5 6

sn为1的报文在数组中仅有1个,下标是2,因此输出2 2

参考题解

模拟。找到最小值,实际上是从从小开始,从最小值开始的第一个是起点,然后是终点。

C++:[此代码未进行大量数据的测试,仅供参考]

#include #include using namespace std; int main() { int n, search_number; cin >> n; vector numbers(n, -1); for (int i = 0; i < n; i++) cin >> numbers[i]; cin >> search_number; int min_index = 0; int max_value = numbers[0]; for (int i = 0; i < n; i++) { if (numbers[i] < max_value) { max_value = numbers[i]; min_index = i; } } int start_index = -1; int end_index = -1; for (int i = 0; i < n; i++) { int current_index = (min_index + i) % n; if (numbers[current_index] == search_number) { if (start_index == -1) { start_index = current_index; end_index = current_index; } else { end_index = current_index; } } } cout numCols; grid.resize(numRows, vector(numCols)); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { cin >> grid[i][j]; } } vector directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int result = INT_MAX; for (int i = 0; i < numRows; i++) { if (grid[i][0] == 1) { queue q; q.push({0, i, 0}); vector used(numRows, vector(numCols, false)); used[i][0] = true; while (!q.empty()) { vector current = q.front(); q.pop(); int distance = current[0]; int x = current[1]; int y = current[2]; if (y == numCols - 1) { result = min(result, distance); } for (vector dir : directions) { int nx = x + dir[0]; int ny = y + dir[1]; if (isValid(nx, ny) && !used[nx][ny] && grid[nx][ny] == 1) { used[nx][ny] = true; q.push({distance + 1, nx, ny}); } } } } }

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

2023 秋招笔试题汇总解析 文章被收录于专栏

2023秋招各大笔试题汇总,c++,java,python多种语言分析,解答。

提示


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有