news 2026/4/15 6:36:38

华为OD机试 - 停车场收入统计 - 数据结构Map(Python/JS/C/C++ 新系统 100分)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
华为OD机试 - 停车场收入统计 - 数据结构Map(Python/JS/C/C++ 新系统 100分)

华为OD机试 新系统 统一考试题库清单(持续收录中)以及考点说明(Python/JS/C/C++)。

专栏导读

本专栏收录于《华为OD机试真题(Python/JS/C/C++)》。

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。

一、题目描述

给定一个停车场某一天的车辆出入记录,请计算该停车场的当日收入,收费规则如下:

• 停车半小时收费1元,不足半小时按半小时计算。

• 一辆车每天收费封顶15元。

• 停车时间小于半小时则不收费

• 11:30 - 13:30 不计入停车时间。

• 包月的车辆不统计当日收入。

二、输入描述

第一段是购买了包月服务的车辆信息:第一行是包月车辆的数量,从第二行开始,每行是一个包月的车牌号。

第二段是当日车辆出入记录:格式为:时(24小时制):分 车牌号(6~10位) enter/leave

三、输出描述

停车场当日收入,整数。

备注
假设所有车辆在当天进当天离。每天的车辆出入记录<10000条,包月车辆个数小于10000。

四、测试用例

测试用例1:

1、输入

1
A12345
01:28 A12345 enter
01:59 A12345 leave

2、输出

0

3、说明

该车是包月车,不计入收入。

测试用例2:

1、输入

0
08:00 A12346 enter
11:20 A12345 enter
11:30 A12346 leave
12:00 A12345 leave

2、输出

7

3、说明

A12346:08:00~11:30,共 210 分钟,收费 210/30=7 元。

A12345:11:20~12:00,共 40 分钟,但 11:30~12:00 这 30 分钟免费,所以有效时长只有 10 分钟,小于 30 分钟,不收费。

总收入 = 7。

五、解题思路

  1. 读取包月车列表,存入 HashSet。
  2. 按行读取当天的出入记录,直到输入结束。
  3. 遇到 enter:记录该车的入场时间。
  4. 遇到 leave:找到对应的入场时间;
    • 若是包月车,直接忽略;
    • 否则计算本次停车费用;
    • 将费用累加到该车当天总费用中,最多 15 元。
  5. 最后把所有非包月车辆的费用求和输出。

六、Python算法源码

importsysdefto_minutes(time_str):# 将 HH:MM 转成分钟数h,m=map(int,time_str.split(":"))returnh*60+mdefcalc_once_fee(enter,leave):# 免费时段 [11:30, 13:30)free_start=11*60+30free_end=13*60+30total=leave-enteriftotal<=0:return0# 计算与免费时段重叠的分钟数overlap_start=max(enter,free_start)overlap_end=min(leave,free_end)overlap=max(0,overlap_end-overlap_start)# 实际计费时长charge_minutes=total-overlap# 小于 30 分钟不收费ifcharge_minutes<30:return0# 向上取整计算半小时数return(charge_minutes+29)//30defsolve(lines):ifnotlines:return"0"idx=0n=int(lines[idx].strip())idx+=1# 包月车辆集合monthly=set()for_inrange(n):monthly.add(lines[idx].strip())idx+=1# 记录最近一次入场时间enter_map={}# 记录每辆车当天累计费用fee_map={}whileidx<len(lines):line=lines[idx].strip()idx+=1ifnotline:continuetime_str,plate,action=line.split()cur_time=to_minutes(time_str)ifaction=="enter":enter_map[plate]=cur_timeelifaction=="leave":ifplatenotinenter_map:continueenter_time=enter_map.pop(plate)# 包月车不收费ifplateinmonthly:continueonce_fee=calc_once_fee(enter_time,cur_time)# 每辆车当天封顶 15 元fee_map[plate]=min(15,fee_map.get(plate,0)+once_fee)returnstr(sum(fee_map.values()))if__name__=="__main__":print(solve(sys.stdin.read().splitlines()),end="")

七、JavaScript算法源码

constfs=require('fs');functiontoMinutes(timeStr){// 将 HH:MM 转为分钟数constarr=timeStr.split(':');consth=parseInt(arr[0],10);constm=parseInt(arr[1],10);returnh*60+m;}functioncalcOnceFee(enter,leave){// 免费时段 [11:30, 13:30)constfreeStart=11*60+30;constfreeEnd=13*60+30;consttotal=leave-enter;if(total<=0)return0;// 计算免费时段重叠分钟数constoverlapStart=Math.max(enter,freeStart);constoverlapEnd=Math.min(leave,freeEnd);constoverlap=Math.max(0,overlapEnd-overlapStart);constchargeMinutes=total-overlap;// 小于 30 分钟不收费if(chargeMinutes<30)return0;// 向上取整returnMath.floor((chargeMinutes+29)/30);}functionsolve(input){constlines=input.split(/\r?\n/).filter(line=>line.trim().length>0);if(lines.length===0)return'0';letidx=0;constn=parseInt(lines[idx].trim(),10);idx++;// 包月车集合constmonthly=newSet();for(leti=0;i<n;i++){monthly.add(lines[idx].trim());idx++;}// 最近一次入场时间constenterMap=newMap();// 当天累计费用constfeeMap=newMap();while(idx<lines.length){constparts=lines[idx].trim().split(/\s+/);idx++;if(parts.length<3)continue;consttimeStr=parts[0];constplate=parts[1];constaction=parts[2];constcurTime=toMinutes(timeStr);if(action==='enter'){enterMap.set(plate,curTime);}elseif(action==='leave'){if(!enterMap.has(plate))continue;constenterTime=enterMap.get(plate);enterMap.delete(plate);// 包月车不收费if(monthly.has(plate))continue;constonceFee=calcOnceFee(enterTime,curTime);// 每辆车当天累计封顶 15 元constcurFee=feeMap.has(plate)?feeMap.get(plate):0;feeMap.set(plate,Math.min(15,curFee+onceFee));}}letans=0;for(constfeeoffeeMap.values()){ans+=fee;}returnString(ans);}process.stdout.write(solve(fs.readFileSync(0,'utf8')));

八、C算法源码

#include<stdio.h>#include<string.h>#include<stdlib.h>#defineMAX_CARS20050#definePLATE_LEN32/** * 由于 C 没有现成的 HashMap / HashSet, * 这里用开放寻址法手写一个简单哈希表。 */typedefstruct{charplate[PLATE_LEN];intvalue;intused;}Node;Node monthlySet[MAX_CARS];Node enterMap[MAX_CARS];Node feeMap[MAX_CARS];/* 计算字符串哈希值 */unsignedinthashStr(constchar*s){unsignedinth=0;while(*s){h=h*131u+(unsignedchar)(*s++);}returnh%MAX_CARS;}/* 查找或插入位置 */intfindIndex(Node table[],constchar*plate){unsignedintidx=hashStr(plate);while(table[idx].used){if(strcmp(table[idx].plate,plate)==0){return(int)idx;}idx=(idx+1)%MAX_CARS;}return(int)idx;}/* 设置键值 */voidsetValue(Node table[],constchar*plate,intvalue){intidx=findIndex(table,plate);if(!table[idx].used){table[idx].used=1;strcpy(table[idx].plate,plate);}table[idx].value=value;}/* 判断键是否存在 */inthasKey(Node table[],constchar*plate){unsignedintidx=hashStr(plate);while(table[idx].used){if(strcmp(table[idx].plate,plate)==0)return1;idx=(idx+1)%MAX_CARS;}return0;}/* 获取键值 */intgetValue(Node table[],constchar*plate,intdefaultValue){unsignedintidx=hashStr(plate);while(table[idx].used){if(strcmp(table[idx].plate,plate)==0)returntable[idx].value;idx=(idx+1)%MAX_CARS;}returndefaultValue;}/* 将 HH:MM 转成分钟 */inttoMinutes(constchar*timeStr){inth=(timeStr[0]-'0')*10+(timeStr[1]-'0');intm=(timeStr[3]-'0')*10+(timeStr[4]-'0');returnh*60+m;}/* 计算一次停车费用 */intcalcOnceFee(intenter,intleave){intfreeStart=11*60+30;intfreeEnd=13*60+30;inttotal=leave-enter;if(total<=0)return0;/* 计算与免费时段重叠部分 */intoverlapStart=enter>freeStart?enter:freeStart;intoverlapEnd=leave<freeEnd?leave:freeEnd;intoverlap=overlapEnd>overlapStart?(overlapEnd-overlapStart):0;intchargeMinutes=total-overlap;/* 小于 30 分钟不收费 */if(chargeMinutes<30)return0;/* 向上取整 */return(chargeMinutes+29)/30;}intmain(){intn;if(scanf("%d",&n)!=1){printf("0");return0;}/* 读取包月车辆 */for(inti=0;i<n;i++){charplate[PLATE_LEN];scanf("%s",plate);setValue(monthlySet,plate,1);}chartimeStr[16],plate[PLATE_LEN],action[16];while(scanf("%s %s %s",timeStr,plate,action)==3){intcurTime=toMinutes(timeStr);if(strcmp(action,"enter")==0){/* 记录入场时间 */setValue(enterMap,plate,curTime);}elseif(strcmp(action,"leave")==0){/* 没有 enter 记录则忽略 */if(!hasKey(enterMap,plate))continue;intenterTime=getValue(enterMap,plate,-1);/* 包月车不收费 */if(hasKey(monthlySet,plate))continue;intonceFee=calcOnceFee(enterTime,curTime);/* 每辆车当天收费封顶 15 元 */intcurFee=getValue(feeMap,plate,0);curFee+=onceFee;if(curFee>15)curFee=15;setValue(feeMap,plate,curFee);}}intans=0;for(inti=0;i<MAX_CARS;i++){if(feeMap[i].used){ans+=feeMap[i].value;}}printf("%d",ans);return0;}

九、C++算法源码

#include<bits/stdc++.h>usingnamespacestd;/* 将 HH:MM 转成分钟数 */inttoMinutes(conststring&timeStr){inth=stoi(timeStr.substr(0,2));intm=stoi(timeStr.substr(3,2));returnh*60+m;}/* 计算一次停车费用 */intcalcOnceFee(intenter,intleave){// 免费时段 [11:30, 13:30)intfreeStart=11*60+30;intfreeEnd=13*60+30;inttotal=leave-enter;if(total<=0)return0;// 计算与免费时段重叠时长intoverlapStart=max(enter,freeStart);intoverlapEnd=min(leave,freeEnd);intoverlap=max(0,overlapEnd-overlapStart);intchargeMinutes=total-overlap;// 小于 30 分钟不收费if(chargeMinutes<30)return0;// 向上取整return(chargeMinutes+29)/30;}intmain(){ios::sync_with_stdio(false);cin.tie(nullptr);intn;if(!(cin>>n)){cout<<0;return0;}// 包月车集合unordered_set<string>monthly;for(inti=0;i<n;i++){string plate;cin>>plate;monthly.insert(plate);}// 记录最近一次入场时间unordered_map<string,int>enterMap;// 记录每辆车当天累计费用unordered_map<string,int>feeMap;string timeStr,plate,action;while(cin>>timeStr>>plate>>action){intcurTime=toMinutes(timeStr);if(action=="enter"){enterMap[plate]=curTime;}elseif(action=="leave"){autoit=enterMap.find(plate);if(it==enterMap.end())continue;intenterTime=it->second;enterMap.erase(it);// 包月车不收费if(monthly.count(plate))continue;intonceFee=calcOnceFee(enterTime,curTime);// 每辆车当天费用封顶 15 元feeMap[plate]=min(15,feeMap[plate]+onceFee);}}intans=0;for(auto&p:feeMap){ans+=p.second;}cout<<ans;return0;}

🏆下一篇:华为OD机试真题 - 简易内存池(Python/JS/C/C++ 新系统 200分)

🏆本文收录于,华为OD机试真题(Python/JS/C/C++)

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/15 6:31:19

从设备树到驱动:platform_get_resource如何解析reg与irq资源

1. 设备树与驱动开发的基础概念 在嵌入式Linux开发中&#xff0c;设备树&#xff08;Device Tree&#xff09;和驱动程序的配合使用是一个非常重要的环节。设备树就像是一张硬件地图&#xff0c;它详细描述了系统中所有硬件设备的配置信息。而驱动程序则是操作这些硬件的软件接…

作者头像 李华
网站建设 2026/4/15 6:27:11

静态语言与动态语言基础:核心区别对比

文章目录 前言一、先搞懂&#xff1a;什么是静态语言&#xff1f;什么是动态语言&#xff1f;1.1 一句话核心定义1.2 2026年主流语言分类&#xff08;真实可查&#xff09;静态类型语言&#xff08;代表&#xff09;动态类型语言&#xff08;代表&#xff09; 二、底层核心区别…

作者头像 李华
网站建设 2026/4/15 6:26:09

企业文件外发最后一公里失控怎么办

文件发给客户的那一刻&#xff0c;你以为工作结束了&#xff1f; 太天真了。 某工程公司的项目经理老周跟我讲过一件事。他们给甲方发了一份标书&#xff0c;报价 480 万&#xff0c;文件通过邮件附件发出。三天后&#xff0c;甲方一个基层办事员把文件转发给了自己的供应商&qu…

作者头像 李华
网站建设 2026/4/15 6:09:24

‌现货库存TMDS1204RNQR‌ 是TI推出的一款高性能 HDMI 2.1 混合转接驱动器,专为支持 ‌高达 12Gbps 数据速率‌ 的视频传输系统设计,广泛适用于高清显示设备与扩展应用

‌TMDS1204RNQR‌ 是德州仪器&#xff08;TI&#xff09;推出的一款高性能 HDMI 2.1 混合转接驱动器&#xff0c;专为支持 ‌高达 12Gbps 数据速率‌ 的视频传输系统设计&#xff0c;广泛适用于高清显示设备与扩展应用。核心产品特性&#xff1a;✅ ‌高速数据支持‌&#xff1…

作者头像 李华
网站建设 2026/4/15 6:07:47

深入解析:pandas为何依赖openpyxl及常见报错处理

1. 为什么pandas读取xlsx文件需要openpyxl&#xff1f; 第一次用pandas处理Excel文件时&#xff0c;我也被这个报错搞懵过。明明只是简单调用了pd.read_excel()&#xff0c;怎么就突然要求安装openpyxl了&#xff1f;这得从xlsx文件的本质说起。 xlsx是Office 2007之后采用的开…

作者头像 李华