全网最全面的华为OD机试真题汇总,100%原题题库,不需要开会员即可查看全部内容,更多考题请查看真题库。
真题库:https://www.yuque.com/codernav.com/od
题目:寻找核酸检测点
时间限制:1s 空间限制:256MB 限定语言:不限
题目描述:
张三要去外地出差,需要做核酸,需要在指定时间点前做完核酸,请帮他找到满足条件的核酸检测点。
1、给出一组核酸检测点的距离和每个核酸检测点当前的人数。
2、给出张三当前要去做核酸的出发时间,出发时间是10分钟的倍数,同时给出张三做核酸的最晚结束时间。
3、题目中给出的距离是整数,单位是公里,时间1分钟为一基本单位。
去找核酸点时,有如下的限制:
1、去往核酸点的路上,每公里距离花费时间10分钟,费用是10元。
2、核酸点每检查一个人的时间花费是1分钟。
3、每个核酸点工作时间都是8点到20点(中间不休息)。核酸点准时工作,早到晚到都不检测。
4、核酸检测结果可立刻知道。
5、在张三去某个核酸点的路上花费的时间内,此核酸检测点的人数是动态变化的,变化的规则是:
5.1 在非核酸检测时间内,没有人排队。
5.2 8点-10点每分钟增加3人。
5.3 12点-14点每分钟增加10人。
5.4 18点-20点每分钟增加20人。
5.5 其他时间每5分钟增加1人。
要求将所有满足条件的核酸检测点按照优选规则排序列出:
优选规则:
1、花费时间最少的核酸检测点排在前面。
2、花费时间一样,花费费用最少的核酸检测点排在前面。
3、时间和费用一样,则ID值最小的排在前面。
输入描述:
H1 M1
H2 M2
N
ID1 D1 C1
ID2 D2 C2
…
IDn Dn Cn
H1:当前时间的小时数。
M1:当前时间的分钟数。
H2: 指定完成核酸时间的小时数。
M2:指定完成核酸时间的分钟数。
N:所有的核酸检测点个数。
ID1:核酸点的ID值。
D1:核酸检测点距离张三的距离。
C1:核酸检测点当前检测的人数。
输出描述:
N
I2 T2 M2
I3 T3 M3
N:满足要求的核酸检测点个数。
I2:选择后的核酸点ID值。
T2:做完核素花费的总共时间(分钟)。
M3:去改核酸点花费的费用。
示例1
输入:
10 30
14 50
3
1 10 19
2 8 20
3 21 3
输出:
2
2 80 80
1 190 100
解题思路:
本题的正确率也是0%,也就是说目前没有人获得满分,只看到了一个真机用例:
8 30
14 50
3
1 10 19
2 8 20
3 21 3
这道题其实就是求张三到达核酸地点时的排队人数;排队人数是分时间段的,我这里是将时间分钟化,这样计算起来比较方便。
代码实现:
package com.codernav.demo.hwod.exam;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @title 寻找核酸检测点
* @Description 张三要去外地出差,需要做核酸,需要在指定时间点前做完核酸,请帮他找到满足条件的核酸检测点。
* 1、给出一组核酸检测点的距离和每个核酸检测点当前的人数。
* 2、给出张三当前要去做核酸的出发时间,出发时间是10分钟的倍数,同时给出张三做核酸的最晚结束时间。
* 3、题目中给出的距离是整数,单位是公里,时间1分钟为一基本单位。
* @Author 开发者导航
* @website https://codernav.com
* @date 2023/5/28
*/
public class Main {
public static int startTime;
public static int endTime;
public static List<int[]> resList = new ArrayList<>();
public static int eightClock = 8 * 60;
public static int tenClock = 10 * 60;
public static int twelveClock = 12 * 60;
public static int fourteenClock = 14 * 60;
public static int eighteenClock = 18 * 60;
public static int twentyClock = 20 * 60;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//当前时间
startTime = sc.nextInt() * 60 + sc.nextInt();
//指定时间
endTime = sc.nextInt() * 60 + sc.nextInt();
//核酸检测点的个数
int N = sc.nextInt();
List<Hesuan> list = new ArrayList<>();
for (int i = 0; i < N; i++) {
list.add(new Hesuan(sc.nextInt(), sc.nextInt(), sc.nextInt()));
}
for (Hesuan hesuan : list) {
handle(hesuan);
}
resList.sort((a, b) -> {
if (a[1] == b[1]) {
if (a[2] == b[2]) {
return a[0] - b[0];
} else {
return a[2] - b[2];
}
}
return a[1] - b[1];
});
System.out.println(resList.size());
for (int[] ints : resList) {
System.out.println(ints[0] + " " + ints[1] + " " + ints[2]);
}
}
public static void handle(Hesuan hesuan) {
//到达核酸地点的路费
int money = hesuan.dance * 10;
//到达核酸地点的时间
int arriveTime = startTime + hesuan.dance * 10;
//到达核酸点时的排队人员
int person = countPerson(startTime, arriveTime, hesuan.persons);
//达到排队的人数+到达的时间=做核酸的时间(1分钟做1人)
int doTime = person + arriveTime;
//满足时间要求
if (doTime <= endTime) {
resList.add(new int[]{hesuan.id, doTime - startTime, money});
}
}
public static int countPerson(int stTime, int eTime, int person) {
//用来计时5分钟加1人
int second = 1;
for (int i = stTime; i < eTime; i++) {
if (i >= eightClock && i < tenClock) {
person += 2;
} else if (i >= twelveClock && i < fourteenClock) {
person += 9;
} else if (i >= eighteenClock && i < twentyClock) {
person += 19;
} else if (second == 5) {
second = 1;
} else {
second++;
if (person > 0) person--; //有人等待则减1人
}
}
return person;
}
public static class Hesuan {
int id;
int dance;
int persons;
public Hesuan(int id, int dance, int persons) {
this.id = id;
this.dance = dance;
this.persons = persons;
}
}
}
