L1-069 胎压监测(15分)[java][python]
·
题目ID:L1-069
分数:15分
语言:Java / Python
题目描述
小轿车中有一个系统随时监测四个车轮的胎压,如果四轮胎压不是很平衡,则可能对行车造成严重的影响。
四个车轮编号:左前轮(1)、右前轮(2)、右后轮(3)、左后轮(4)
报警规则
- 正常情况:所有轮胎压力值与最大值误差在阈值内,且都不低于最低报警胎压 → 不报警
- 单个轮胎异常:存在1个轮胎的压力值与最大值误差超过阈值,或低于最低报警胎压 → 报警并指出具体轮胎编号
- 多个轮胎异常:存在2个或更多轮胎异常 → 报警要求检查所有轮胎
输入格式
一行6个整数(范围[0, 400]):
1~4号轮胎胎压 最低报警胎压 胎压差阈值
输出格式
| 情况 | 输出 |
|---|---|
| 正常 | Normal |
| 单轮胎异常 | Warning: please check #X! |
| 多轮胎异常 | Warning: please check all the tires! |
输入样例
242 251 231 248 230 20
输出样例
Normal
解题思路
- 求最大值:遍历数组找出四个轮胎中的最大胎压
- 判断异常:对每个轮胎检查两个条件:
max - tire > threshold→ 与最大值差距超过阈值tire < minPressure→ 低于最低报警胎压
- 计数输出:根据异常轮胎数量决定输出格式
代码实现
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] tires = new int[4];
for (int i = 0; i < 4; i++) {
tires[i] = scanner.nextInt();
}
int minPressure = scanner.nextInt(); // 最低报警胎压
int threshold = scanner.nextInt(); // 胎压差阈值
// 找最大值
int max = tires[0];
for (int i = 1; i < 4; i++) {
if (tires[i] > max) {
max = tires[i];
}
}
// 检查异常轮胎
int count = 0;
int lastAbnormal = -1;
for (int i = 0; i < 4; i++) {
if (max - tires[i] > threshold || tires[i] < minPressure) {
count++;
lastAbnormal = i + 1; // 记录最后一个异常轮胎编号
}
}
// 输出结果
if (count == 0) {
System.out.println("Normal");
} else if (count == 1) {
System.out.println("Warning: please check #" + lastAbnormal + "!");
} else {
System.out.println("Warning: please check all the tires!");
}
}
}
Python
tires = list(map(int, input().split()))
min_pressure, threshold = map(int, input().split())
# 找最大值
max_pressure = max(tires)
# 检查异常轮胎
abnormal = []
for i, t in enumerate(tires):
if max_pressure - t > threshold or t < min_pressure:
abnormal.append(i + 1) # 轮胎编号从1开始
count = len(abnormal)
# 输出结果
if count == 0:
print("Normal")
elif count == 1:
print(f"Warning: please check #{abnormal[0]}!")
else:
print("Warning: please check all the tires!")
运行验证
| 样例输入 | 样例输出 | 结果 |
|---|---|---|
| 242 251 231 248 230 20 | Normal | ✅ |
| 242 251 232 248 230 10 | Warning: please check #3! | ✅ |
| 240 251 232 248 240 10 | Warning: please check all the tires! | ✅ |
样例1解释:最大值251,阈值20。231与251差20(等于阈值),248差3,242差9,都满足条件且≥230,正常。
样例2解释:阈值10,232与251差19>10,轮胎3异常。
复杂度分析
- 时间复杂度:O(1)
- 空间复杂度:O(1)
总结
本题考察数组遍历和条件判断:
- 找出数组最大值
- 遍历检查异常情况
- 根据异常数量决定输出格式
更多推荐


所有评论(0)