华为OD机试 2025A卷题库疯狂收录中,刷题点这里
专栏导读
本专栏收录于《华为OD机试真题(Python/JS/C/C++)》。
刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。
一、题目描述
小明今年升学到了小学1年级来到新班级后,发现其他小朋友身高参差不文,然后就想基于各小朋友和自己的身高差,对他们进行排序,请帮他实现排序。
二、输入描述
第一行为正整数h和n,0<h<200为小明的身高,0<n<50为新班级其他小朋友个数;
第二行为n个正整数,h1~hn分别是其他小朋友的身高,取值范围0<hi<200,且n个正整数;各不相同。
三、输出描述
输出排序结果,各正整数以空格分割和小明身高差绝对值最小的小朋友排在前面,
和小明身高差绝对值最大的小朋友排在后面。
如果两个小朋友和小明身高差一样,则个子较小的小朋友排在前面。
四、测试用例
测试用例1:
1、输入
150 5
140 155 160 145 150
2、输出
150 145 155 140 160
3、说明
身高差:
140: 10
155: 5 160: 10
145: 5
150: 0
排序结果:150 (差0), 145 (差5), 155 (差5), 140 (差10), 160 (差10)
测试用例2:
1、输入
120 4
130 110 125 115
2、输出
115 125 110 130
3、说明
身高差:
130: 10
110: 10
125: 5
115: 5
排序结果:115 (差5), 125 (差5), 110 (差10), 130 (差10)
五、解题思路
- 第一行为正整数h和n,小明的身高、新班级其他小朋友个数;
- 定义集合list,存储新班级其他小朋友的身高;
- 以空格分割和小明身高差绝对值最小的小朋友排在前面,和小明身高差绝对值最大的小朋友排在后面;
- 如果两个小朋友和小明身高差一样,则个子较小的小朋友排在前面;
- 空格分隔并输出。
六、Python算法源码
# 导入必要的模块
import sys
def main():
# 读取第一行输入并拆分为h和n
line1 = sys.stdin.readline().strip().split()
h = int(line1[0]) # 小明的身高
n = int(line1[1]) # 其他小朋友的数量
# 读取第二行输入并转换为整数列表
heights = list(map(int, sys.stdin.readline().strip().split()))
# 定义排序的关键函数
# 首先按照与h的绝对差排序,然后按照身高排序
sorted_heights = sorted(heights, key=lambda x: (abs(x - h), x))
# 将排序后的结果转换为字符串并输出
print(' '.join(map(str, sorted_heights)))
if __name__ == "__main__":
main()
七、JavaScript算法源码
// 读取标准输入并处理
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let inputLines = [];
rl.on('line', (line) => {
inputLines.push(line);
if (inputLines.length === 2) {
rl.close();
}
}).on('close', () => {
// 解析第一行输入
const [h, n] = inputLines[0].split(' ').map(Number);
// 解析第二行输入
let heights = inputLines[1].split(' ').map(Number);
// 定义排序规则
heights.sort((a, b) => {
let diffA = Math.abs(a - h);
let diffB = Math.abs(b - h);
if (diffA === diffB) {
return a - b; // 身高相同则按身高排序
}
return diffA - diffB; // 按差值排序
});
// 输出排序结果
console.log(heights.join(' '));
});
八、C算法源码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// 定义一个结构体来存储身高及其与h的差值
typedef struct {
int height;
int diff;
} Child;
// 比较函数,用于排序
int compare(const void *a, const void *b, void *h_ptr) {
int h = *(int *)h_ptr;
Child *childA = (Child *)a;
Child *childB = (Child *)b;
if (childA->diff != childB->diff) {
return childA->diff - childB->diff;
} else {
return childA->height - childB->height;
}
}
int main() {
int h, n;
// 读取h和n
scanf("%d %d", &h, &n);
Child children[n];
// 读取n个小朋友的身高并计算差值
for(int i = 0; i < n; i++) {
scanf("%d", &children[i].height);
children[i].diff = abs(children[i].height - h);
}
// 使用qsort_r进行排序,传入h作为额外参数
// 注意:qsort_r的使用在不同系统中可能有所不同,此处以GNU为例
qsort_r(children, n, sizeof(Child), compare, &h);
// 输出排序结果
for(int i = 0; i < n; i++) {
printf("%d", children[i].height);
if(i != n -1) printf(" ");
}
printf("\n");
return 0;
}
九、C++算法源码
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int h, n;
// 读取小明的身高和小朋友的数量
cin >> h >> n;
vector<int> heights(n);
// 读取n个小朋友的身高
for(int &height : heights){
cin >> height;
}
// 定义排序规则
sort(heights.begin(), heights.end(), [&](const int a, const int b) -> bool{
int diffA = abs(a - h);
int diffB = abs(b - h);
if(diffA != diffB){
return diffA < diffB; // 按差值排序
}
return a < b; // 差值相同则按身高排序
});
// 输出排序结果
for(int i = 0; i < n; ++i){
cout << heights[i];
if(i != n -1) cout << ' ';
}
cout << '\n';
return 0;
}
🏆下一篇:华为OD机试真题 - 简易内存池(Python/JS/C/C++ 2025 A卷 200分)
🏆本文收录于,华为OD机试真题(Python/JS/C/C++)
刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。