华为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。四、测试用例测试用例11、输入1A1234501:28 A12345 enter01:59 A12345 leave2、输出03、说明该车是包月车不计入收入。测试用例21、输入008:00 A12346 enter11:20 A12345 enter11:30 A12346 leave12:00 A12345 leave2、输出73、说明A1234608:00~11:30共 210 分钟收费 210/307 元。A1234511:20~12:00共 40 分钟但 11:30~12:00 这 30 分钟免费所以有效时长只有 10 分钟小于 30 分钟不收费。总收入 7。五、解题思路读取包月车列表存入 HashSet。按行读取当天的出入记录直到输入结束。遇到 enter记录该车的入场时间。遇到 leave找到对应的入场时间若是包月车直接忽略否则计算本次停车费用将费用累加到该车当天总费用中最多 15 元。最后把所有非包月车辆的费用求和输出。六、Python算法源码importsysdefto_minutes(time_str):# 将 HH:MM 转成分钟数h,mmap(int,time_str.split(:))returnh*60mdefcalc_once_fee(enter,leave):# 免费时段 [11:30, 13:30)free_start11*6030free_end13*6030totalleave-enteriftotal0:return0# 计算与免费时段重叠的分钟数overlap_startmax(enter,free_start)overlap_endmin(leave,free_end)overlapmax(0,overlap_end-overlap_start)# 实际计费时长charge_minutestotal-overlap# 小于 30 分钟不收费ifcharge_minutes30:return0# 向上取整计算半小时数return(charge_minutes29)//30defsolve(lines):ifnotlines:return0idx0nint(lines[idx].strip())idx1# 包月车辆集合monthlyset()for_inrange(n):monthly.add(lines[idx].strip())idx1# 记录最近一次入场时间enter_map{}# 记录每辆车当天累计费用fee_map{}whileidxlen(lines):linelines[idx].strip()idx1ifnotline:continuetime_str,plate,actionline.split()cur_timeto_minutes(time_str)ifactionenter:enter_map[plate]cur_timeelifactionleave:ifplatenotinenter_map:continueenter_timeenter_map.pop(plate)# 包月车不收费ifplateinmonthly:continueonce_feecalc_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算法源码constfsrequire(fs);functiontoMinutes(timeStr){// 将 HH:MM 转为分钟数constarrtimeStr.split(:);consthparseInt(arr[0],10);constmparseInt(arr[1],10);returnh*60m;}functioncalcOnceFee(enter,leave){// 免费时段 [11:30, 13:30)constfreeStart11*6030;constfreeEnd13*6030;consttotalleave-enter;if(total0)return0;// 计算免费时段重叠分钟数constoverlapStartMath.max(enter,freeStart);constoverlapEndMath.min(leave,freeEnd);constoverlapMath.max(0,overlapEnd-overlapStart);constchargeMinutestotal-overlap;// 小于 30 分钟不收费if(chargeMinutes30)return0;// 向上取整returnMath.floor((chargeMinutes29)/30);}functionsolve(input){constlinesinput.split(/\r?\n/).filter(lineline.trim().length0);if(lines.length0)return0;letidx0;constnparseInt(lines[idx].trim(),10);idx;// 包月车集合constmonthlynewSet();for(leti0;in;i){monthly.add(lines[idx].trim());idx;}// 最近一次入场时间constenterMapnewMap();// 当天累计费用constfeeMapnewMap();while(idxlines.length){constpartslines[idx].trim().split(/\s/);idx;if(parts.length3)continue;consttimeStrparts[0];constplateparts[1];constactionparts[2];constcurTimetoMinutes(timeStr);if(actionenter){enterMap.set(plate,curTime);}elseif(actionleave){if(!enterMap.has(plate))continue;constenterTimeenterMap.get(plate);enterMap.delete(plate);// 包月车不收费if(monthly.has(plate))continue;constonceFeecalcOnceFee(enterTime,curTime);// 每辆车当天累计封顶 15 元constcurFeefeeMap.has(plate)?feeMap.get(plate):0;feeMap.set(plate,Math.min(15,curFeeonceFee));}}letans0;for(constfeeoffeeMap.values()){ansfee;}returnString(ans);}process.stdout.write(solve(fs.readFileSync(0,utf8)));八、C算法源码#includestdio.h#includestring.h#includestdlib.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){unsignedinth0;while(*s){hh*131u(unsignedchar)(*s);}returnh%MAX_CARS;}/* 查找或插入位置 */intfindIndex(Node table[],constchar*plate){unsignedintidxhashStr(plate);while(table[idx].used){if(strcmp(table[idx].plate,plate)0){return(int)idx;}idx(idx1)%MAX_CARS;}return(int)idx;}/* 设置键值 */voidsetValue(Node table[],constchar*plate,intvalue){intidxfindIndex(table,plate);if(!table[idx].used){table[idx].used1;strcpy(table[idx].plate,plate);}table[idx].valuevalue;}/* 判断键是否存在 */inthasKey(Node table[],constchar*plate){unsignedintidxhashStr(plate);while(table[idx].used){if(strcmp(table[idx].plate,plate)0)return1;idx(idx1)%MAX_CARS;}return0;}/* 获取键值 */intgetValue(Node table[],constchar*plate,intdefaultValue){unsignedintidxhashStr(plate);while(table[idx].used){if(strcmp(table[idx].plate,plate)0)returntable[idx].value;idx(idx1)%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*60m;}/* 计算一次停车费用 */intcalcOnceFee(intenter,intleave){intfreeStart11*6030;intfreeEnd13*6030;inttotalleave-enter;if(total0)return0;/* 计算与免费时段重叠部分 */intoverlapStartenterfreeStart?enter:freeStart;intoverlapEndleavefreeEnd?leave:freeEnd;intoverlapoverlapEndoverlapStart?(overlapEnd-overlapStart):0;intchargeMinutestotal-overlap;/* 小于 30 分钟不收费 */if(chargeMinutes30)return0;/* 向上取整 */return(chargeMinutes29)/30;}intmain(){intn;if(scanf(%d,n)!1){printf(0);return0;}/* 读取包月车辆 */for(inti0;in;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){intcurTimetoMinutes(timeStr);if(strcmp(action,enter)0){/* 记录入场时间 */setValue(enterMap,plate,curTime);}elseif(strcmp(action,leave)0){/* 没有 enter 记录则忽略 */if(!hasKey(enterMap,plate))continue;intenterTimegetValue(enterMap,plate,-1);/* 包月车不收费 */if(hasKey(monthlySet,plate))continue;intonceFeecalcOnceFee(enterTime,curTime);/* 每辆车当天收费封顶 15 元 */intcurFeegetValue(feeMap,plate,0);curFeeonceFee;if(curFee15)curFee15;setValue(feeMap,plate,curFee);}}intans0;for(inti0;iMAX_CARS;i){if(feeMap[i].used){ansfeeMap[i].value;}}printf(%d,ans);return0;}九、C算法源码#includebits/stdc.husingnamespacestd;/* 将 HH:MM 转成分钟数 */inttoMinutes(conststringtimeStr){inthstoi(timeStr.substr(0,2));intmstoi(timeStr.substr(3,2));returnh*60m;}/* 计算一次停车费用 */intcalcOnceFee(intenter,intleave){// 免费时段 [11:30, 13:30)intfreeStart11*6030;intfreeEnd13*6030;inttotalleave-enter;if(total0)return0;// 计算与免费时段重叠时长intoverlapStartmax(enter,freeStart);intoverlapEndmin(leave,freeEnd);intoverlapmax(0,overlapEnd-overlapStart);intchargeMinutestotal-overlap;// 小于 30 分钟不收费if(chargeMinutes30)return0;// 向上取整return(chargeMinutes29)/30;}intmain(){ios::sync_with_stdio(false);cin.tie(nullptr);intn;if(!(cinn)){cout0;return0;}// 包月车集合unordered_setstringmonthly;for(inti0;in;i){string plate;cinplate;monthly.insert(plate);}// 记录最近一次入场时间unordered_mapstring,intenterMap;// 记录每辆车当天累计费用unordered_mapstring,intfeeMap;string timeStr,plate,action;while(cintimeStrplateaction){intcurTimetoMinutes(timeStr);if(actionenter){enterMap[plate]curTime;}elseif(actionleave){autoitenterMap.find(plate);if(itenterMap.end())continue;intenterTimeit-second;enterMap.erase(it);// 包月车不收费if(monthly.count(plate))continue;intonceFeecalcOnceFee(enterTime,curTime);// 每辆车当天费用封顶 15 元feeMap[plate]min(15,feeMap[plate]onceFee);}}intans0;for(autop:feeMap){ansp.second;}coutans;return0;}下一篇华为OD机试真题 - 简易内存池Python/JS/C/C 新系统 200分本文收录于华为OD机试真题Python/JS/C/C刷的越多抽中的概率越大私信哪吒备注华为OD加入华为OD刷题交流群每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景发现新题目随时更新。