华为笔试 华为笔试题 0423
笔试时间:2025年04月23日
历史笔试传送门:
第一题
题目:最近亮度坐标查找
给定一张二维图像,图像中每个值表示该坐标下的亮度。给定一个亮度值 m ,返回离图像中心坐标最近的 k 个亮度为 m 值的坐标 (x, y) 。图像相关提示:图像中元素的坐标范围:x: [0, w - 1],y: [0, h - 1]。图像宽高 w、h 均为奇数,图像中心坐标 (w - 1)/2, (h - 1)/2 。平面上两点之间的距离为 |x1 - x2| + |y1 - y2| 。在距离相同的情况下,以 x 小的点优先;当 x 相同时,以 y 小的点优先。题目可保证至少存在一个亮度值为 m 的点。
输入描述
第一行输入为图像宽度 w 和图像高度 h ,以空格隔开,宽高范围为 1 ~ 2000 。
第二行输入为给定亮度值 m ,范围为 1 ~ 1000 。
第三行输入为需要输出的亮度值为 m 的坐标个数 k ,范围为 1 ~ 100,且 k <= w * h 。
接下来共 h 行,每行内为 w 个亮度,以空格隔开,亮度范围为 1 ~ 1000 。
输出描述
按顺序输出坐标序列,坐标 x 和 y 以空格隔开,坐标间使用空格隔开 。x, y 坐标均不相同时以 x 增序排列, x 坐标相同时以 y 增序排列。若满足条件的坐标个数不足 k ,则以实际坐标个数输出。
样例输入
5 5
10
3
10 2 3 4 5
1 2 3 4 10
1 2 3 10 5
1 10 3 4 5
1 2 3 4 5
样例输出
3 2 1 3 4 1
说明:
该用例图像 w =5,h =5 ,给定亮度值 m = 10 ,需要输出 m = 10 的距离中心最近的3个坐标。
该图像中心坐标为(2,2)。
从图像中可以看出,
距离中心坐标最近的 m= 10 的坐标为(3,2),距离为|3-2|+|2-2| = 1;
距离第二近的坐标为(1,3),距离为|1-2|+|3-2|=2
距离第三近的坐标为(4,1),距离为|4-2+|1-2|=3
由上可知,三个坐标为(3,2)、(1,3)、(4,1),按照输出格式要求,输出为
3 2 1 3 4 1
参考题解
直接模拟,首先根据图像的宽高计算图像的中心坐标,遍历图像矩阵,找到所有亮度为 m 的坐标,并计算这些坐标到中心坐标的距离。然后按照距离排序,距离相同的情况下,以 x 小的点优先;当 x 相同时,以 y 小的点优先。输出前 k 个满足条件的坐标即可。
C++:[此代码未进行大量数据的测试,仅供参考]
// C++ 版本 #include <bits/stdc++.h> using namespace std; struct Coord { int x, y, dist; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int w, h; cin >> w >> h; int m, k; cin >> m >> k; vector<vector<int>> image(h, vector<int>(w)); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> image[y][x]; } } int center_x = (w - 1) / 2; int center_y = (h - 1) / 2; vector<Coord> coords; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (image[y][x] == m) { int dist = abs(x - center_x) + abs(y - center_y); coords.push_back({x, y, dist}); } } } sort(coords.begin(), coords.end(), [](const Coord &a, const Coord &b) { if (a.dist != b.dist) return a.dist < b.dist; if (a.x != b.x) return a.x < b.x; return a.y < b.y; }); int cnt = min(k, (int)coords.size()); for (int i = 0; i < cnt; i++) { if (i) cout << ' '; cout << coords[i].x << ' ' << coords[i].y; } cout << '\n'; return 0; }
Java:[此代码未进行大量数据的测试,仅供参考]
// Java 版本 import java.io.*; import java.util.*; public class Main { static class Coord implements Comparable<Coord> { int x, y, dist; Coord(int x, int y, int dist) { this.x = x; this.y = y; this.dist = dist; } @Override public int compareTo(Coord o) { if (this.dist != o.dist) return Integer.compare(this.dist, o.dist); if (this.x != o.x) return Integer.compare(this.x, o.x); return Integer.compare(this.y, o.y); } } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); int w = Integer.parseInt(st.nextToken()); int h = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(in.readLine().trim()); int k = Integer.parseInt(in.readLine().trim()); int[][] image = new int[h][w]; for (int y = 0; y < h; y++) { st = new StringTokenizer(in.readLine()); for (int x = 0; x < w; x++) { image[y][x] = Integer.parseInt(st.nextToken()); } } int centerX = (w - 1) / 2; int centerY = (h - 1) / 2; List<Coord> coords = new ArrayList<>(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (image[y][x] == m) { int dist = Math.abs(x - centerX) + Math.abs(y - centerY); coords.add(new Coord(x, y, dist)); } } } Collections.sort(coords); StringBuilder sb = new StringBuilder(); int cnt = Math.min(k, coords.size()); for (int i = 0; i < cnt; i++) { if (i > 0) sb.append(' '); sb.append(coords.get(i).x).append(' ').append(coords.get(i).y); } System.out.println(sb.toString()); } }
Python:[此代码未进行大量数据的测试,仅供参考]
# 读取输入 w, h = map(int, input().split()) m = int(input()) k = int(input()) image = [] for _ in range(h): row = list(map(int, input().split())) image.append(row) # 计算图像中心坐标 center_x = (w - 1
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南