全网最全面的华为OD机试真题汇总,100%原题题库,不需要开会员即可查看全部内容,更多考题请查看真题库。
真题库:https://www.yuque.com/codernav.com/od
题目:预定酒店
知识点排序
时间限制:1s 空间限制:256MB 限定语言:不限
题目描述:
放暑假了,小明决定到某旅游景点游玩,他在网上搜索到了各种价位的酒店(长度为n的数组A),他的心理价位是x元,请帮他筛选出k个最接近x元的酒店(n>=k>0),并由低到高打印酒店的价格。
备注:
1)酒店价格数组A和小明的心理价位x均为整型数据;(0 < n,k,x < 10000)
2)优先选择价格最接近心理价位的酒店;若两家酒店和心理价位差价相同,则选择价格较低的酒店。(比如100元和300元距离心理价位200元同样接近,此时选择100元);
3)酒店价格可能相同重复。
输入描述:
第一行:n, k, x
第二行:A[0] A[1] A[2]…A[n-1]
输出描述:
由低到高打印筛选出的酒店价格
补充说明:
1)酒店价格数组A和小明的心理价位x均为整型数据
2)优先选择价格最接近心理价位的酒店;若两家酒店距离心理价位差价相同,则选择价格较低的酒店。(比如100元和300元距离心理价位200元同样接近,此时选择100元)
3)酒店价格可能相同重复。
示例1
输入:
10 5 6
1 2 3 4 5 6 7 8 9 10
输出:
4 5 6 7 8
说明:
数组长度n = 10,筛选个数k = 5,目标价位x=6
示例2
输入:
10 4 6
10 9 8 7 6 5 4 3 2 1
输出:
4 5 6 7
说明:
数组长度n = 10,筛选个数k = 4,目标价位x=6
当4和8距离x相同时,优先选择价格低的4
示例3
输入:
6 3 1000
30 30 200 500 70 300
输出:
200 300 500
解题思路:
1、使用hotel类来记录酒店价格和心理差价
2、根据心理差价,从集合中获取合适的酒店,并按照差价进行降序排序
3、根据k值获取集合中的酒店价格,然后对价格进行升序输出
代码实现一:
package com.codernav.demo.hwod.exam;
import java.util.*;
/**
* @title 预定酒店
* @Description 放暑假了,小明决定到某旅游景点游玩,他在网上搜索到了各种价位的酒店(长度为n的数组A),
* 他的心理价位是x元,请帮他筛选出k个最接近x元的酒店(n>=k>0),并由低到高打印酒店的价格。
* @Author 开发者导航
* @website https://codernav.com
* @date 2023/5/14
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int x = sc.nextInt();
int[] A = new int[n];
List<Hotel> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
A[i] = sc.nextInt();
Hotel hotel = new Hotel(A[i], Math.abs(A[i] - x));
list.add(hotel);
}
Collections.sort(list);
int[] resInts = new int[k];
for (int i = 0; i < k; i++) {
resInts[i] = list.get(i).price;
}
Arrays.sort(resInts);
String res = "";
for (int i : resInts) {
res += i + " ";
}
System.out.println(res.substring(0, res.length() - 1));
}
public static class Hotel implements Comparable<Hotel> {
//酒店价格
public int price;
//与心理价格差距
public int difPrice;
public Hotel(int price, int difPrice) {
this.price = price;
this.difPrice = difPrice;
}
@Override
public int compareTo(Hotel hotel) {
return this.difPrice - hotel.difPrice;
}
}
}
代码实现二:
package com.codernav.demo.hwod.exam;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
/**
* @title 预定酒店
* @Description 放暑假了,小明决定到某旅游景点游玩,他在网上搜索到了各种价位的酒店(长度为n的数组A),
* 他的心理价位是x元,请帮他筛选出k个最接近x元的酒店(n>=k>0),并由低到高打印酒店的价格。
* @Author 开发者导航
* @website https://codernav.com
* @date 2023/5/14
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] s = sc.nextLine().split(" ");
int n = Integer.parseInt(s[0]);
int k = Integer.parseInt(s[1]);
int x = Integer.parseInt(s[2]);
Integer[] integers = Arrays.stream(sc.nextLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new);
ArrayList<int[]> res = new ArrayList<>();
for (Integer integer : integers) {
int[] ints = new int[2];
int abs = Math.abs(integer - x);
ints[0] = integer;
ints[1] = abs;
res.add(ints);
}
res.sort((o1, o2) -> {
if (o1[1] == o2[1]) {
return o1[0] - o2[0];
} else {
return o1[1] - o2[1];
}
});
ArrayList<Integer> integers1 = new ArrayList<>();
for (int i = 0; i < k; i++) {
int i1 = res.get(i)[0];
integers1.add(i1);
}
Collections.sort(integers1);
StringBuilder sb = new StringBuilder();
for (Integer integer : integers1) {
sb.append(integer).append(" ");
}
System.out.println(sb.deleteCharAt(sb.length() - 1));
}
}
