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
importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);int[]tires=newint[4];for(inti=0;i<4;i++){tires[i]=scanner.nextInt();}intminPressure=scanner.nextInt();// 最低报警胎压intthreshold=scanner.nextInt();// 胎压差阈值// 找最大值intmax=tires[0];for(inti=1;i<4;i++){if(tires[i]>max){max=tires[i];}}// 检查异常轮胎intcount=0;intlastAbnormal=-1;for(inti=0;i<4;i++){if(max-tires[i]>threshold||tires[i]<minPressure){count++;lastAbnormal=i+1;// 记录最后一个异常轮胎编号}}// 输出结果if(count==0){System.out.println("Normal");}elseif(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=[]fori,tinenumerate(tires):ifmax_pressure-t>thresholdort<min_pressure:abnormal.append(i+1)# 轮胎编号从1开始count=len(abnormal)# 输出结果ifcount==0:print("Normal")elifcount==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)
总结
本题考察数组遍历和条件判断:
- 找出数组最大值
- 遍历检查异常情况
- 根据异常数量决定输出格式
