资讯动态

【笔试题汇总】华为春招笔试题解 2024-4-17

发布时间:2026/9/10 9:48:29 来源:尧图企业网站定制
前些天发现了一个巨牛的人工智能学习网站通俗易懂风趣幽默忍不住分享一下给大家:人工智能学习网这里是paoxiaomo一个现役ACMer之后将会持续更新算法笔记系列以及笔试题题解系列本文章面向想打ICPC/蓝桥杯/天梯赛等程序设计竞赛以及各个大厂笔试的选手感谢大家的订阅➕ 和 喜欢有什么想看的算法专题可以私信博主本文题面由清隆学长收集01.扑克牌消消乐题目描述K小姐最近沉迷于一款扑克牌消除游戏。游戏规则如下从一副扑克牌中随机抽取n nn张牌组成一个序列如果有连续的3 33张相同牌号的卡牌则这3 33张卡牌可以消除。消除后剩余卡牌按照当前顺序重新合并成新的序列继续寻找可以消除的卡牌组合直到无法再消除为止。需要注意的是如果存在连续的4 44张相同牌号的卡牌在消除后会剩余1 11张该牌号的卡牌。现在K小姐想知道最后剩余的卡牌序列是什么样的你能帮助她吗输入格式第一行包含一个正整数n nn1 ≤ n ≤ 52 1 \leq n \leq 521≤n≤52表示抽取的卡牌数量。第二行包含n nn个以空格分隔的字符串表示抽取的卡牌序列卡牌号仅包含2 22-10 1010A AAJ JJQ QQK KK。输出格式输出一个字符串表示最终剩余的卡牌号序列卡牌号之间以空格分隔。如果最终没有卡牌剩余则输出0 00。样例输入10 3 A 2 2 2 A A 7 7 7样例输出3数据范围1 ≤ n ≤ 52 1 \leq n \leq 521≤n≤52卡牌号仅包含2 22-10 1010A AAJ JJQ QQK KK。【题目解析】读取输入包括卡牌数量n nn和n nn张卡牌序列。定义一个函数removeTriples用于消除连续3 33张相同牌号的卡牌并返回消除后的卡牌序列。进入主循环不断调用removeTriples函数直到无法再消除为止即当前序列与上一次序列相同。输出最终剩余的卡牌序列如果没有剩余卡牌则输出 “0”。其中removeTriples函数负责消除连续3 33张相同牌号的卡牌cpp#includeiostream#includestring#includevectorusingnamespacestd;stringremoveTriples(conststringcards){string result;intlencards.length();inti0;while(ilen){intji;while(jlencards[j]cards[i]){j;}if(j-i3){ij;}else{resultcards.substr(i,j-i);ij;}}returnresult;}intmain(){intn;cinn;vectorstringcards(n);for(inti0;in;i){cincards[i];}string current;for(inti0;in;i){currentcards[i];}string prev;while(prev!current){prevcurrent;currentremoveTriples(current);}if(current.empty()){cout0endl;}else{for(charc:current){coutc ;}coutendl;}return0;}02.公司部门风险评估题目描述LYA 是一家大型科技公司的风险评估师。公司的部门结构可以看作一棵树每个部门在评估前都有一些尚未解决的问题。部门的风险值可以用来评估该部门是否存在风险风险值的计算公式为风险值 5 × 严重问题数 2 × 一般问题数 风险值 5 \times 严重问题数 2 \times 一般问题数风险值5×严重问题数2×一般问题数。其中每个部门的不同级别问题数量需要将该部门及其下属部门的相应级别问题数量求和。当部门的风险值小于等于给定的阈值时该部门被认为是安全的否则该部门被视为风险部门需要进一步整改。现在给出公司的部门结构以及各部门的问题数量请你帮助 LYA 计算出风险部门的数量。输入格式第一行包含两个正整数M MM和N NN1 ≤ M ≤ 100000 1 \leq M \leq 1000001≤M≤1000001 ≤ N ≤ 1000 1 \leq N \leq 10001≤N≤1000分别表示风险阈值和部门的数量。接下来N NN行每行包含四个字段用空格分隔第一个字段为部门名称A i A_iAi​第二个字段为A i A_iAi​的上级部门名称B i B_iBi​如果A i A_iAi​为公司的最高层部门则B i B_iBi​用*表示第三个字段为问题级别C i C_iCi​C i ∈ { 0 , 1 } C_i \in \{0, 1\}Ci​∈{0,1}其中0 00表示严重问题1 11表示一般问题第四个字段为该部门该级别的问题数量D i D_iDi​1 ≤ D i ≤ 1000 1 \leq D_i \leq 10001≤Di​≤1000。其中A i A_iAi​和B i B_iBi​为由小写英文字母组成的字符串长度不超过5 55。输入保证部门结构为一棵树不会出现环的情况。输出格式输出一个整数表示风险部门的数量。样例输入40 12 a * 0 2 a * 1 2 b a 0 3 b a 1 5 c a 1 3 d a 0 1 d a 1 3 e b 0 2 f * 0 8 f * 1 10 g f 1 2 h * 0 4样例输出2数据范围1 ≤ M ≤ 100000 1 \leq M \leq 1000001≤M≤1000001 ≤ N ≤ 1000 1 \leq N \leq 10001≤N≤10001 ≤ D i ≤ 1000 1 \leq D_i \leq 10001≤Di​≤1000A i A_iAi​和B i B_iBi​为由小写英文字母组成的字符串长度不超过5 55。【题目解析】读取输入包括风险阈值和部门数量并构建部门树结构。对每个部门计算其风险值如果风险值大于阈值则该部门及其下属部门都被标记为风险部门。统计所有被标记的部门数量即为风险部门的数量。输出风险部门的数量。cpp#includeiostream#includestring#includeunordered_map#includevectorusingnamespacestd;structDepartment{string name;string parent;intsevere_issues;intnormal_issues;vectorstringchildren;};unordered_mapstring,Departmentdepartments;// Function to calculate the risk value of a departmentintcalculateRisk(conststringdepartment_name,intthreshold){Departmentdeptdepartments[department_name];intrisk_value5*dept.severe_issues2*dept.normal_issues;if(risk_valuethreshold){return1;}else{inttotal_risk0;for(conststringchild_name:dept.children){total_riskcalculateRisk(child_name,threshold);}returntotal_risk;}}intmain(){intthreshold,num_departments;cinthresholdnum_departments;for(inti0;inum_departments;i){string name,parent;intissue_type,num_issues;cinnameparentissue_typenum_issues;departments[name]{name,parent,0,0};if(issue_type0){departments[name].severe_issuesnum_issues;}else{departments[name].normal_issuesnum_issues;}if(parent!*){departments[parent].children.push_back(name);}}intrisk_departmentscalculateRisk(*,threshold);coutrisk_departmentsendl;return0;}03.城市应急疏散题目描述LYA 是一名城市应急管理专家她负责制定城市在发生重大事故时的疏散计划。城市由n nn个区域组成每个区域之间都有道路相连。当某个区域发生事故需要疏散时LYA 需要选择一个或多个安全区域作为疏散目的地并确保疏散路径的总长度最短。给定一个n × n n \times nn×n的矩阵d i s t distdist其中d i s t [ i ] [ j ] dist[i][j]dist[i][j]表示区域i ii到区域j jj的道路长度如果d i s t [ i ] [ j ] − 1 dist[i][j] -1dist[i][j]−1则表示区域i ii和区域j jj之间没有直接相连的道路。另外每个区域还有一个剩余容量c a p [ i ] cap[i]cap[i]表示该区域最多可以容纳的人数。当某个区域x xx发生事故需要疏散人数为p pp时请你帮助 LYA 选择疏散区域使得疏散路径的总长度最短并且疏散区域的剩余容量之和不小于p pp。如果有多个疏散区域到事故区域的最短路径长度相同则优先选择编号较小的区域。输入格式第一行包含一个正整数n nn表示区域的数量。接下来n nn行每行包含n nn个整数表示矩阵d i s t distdist。接下来一行包含n nn个整数表示每个区域的剩余容量c a p [ i ] cap[i]cap[i]。最后一行包含两个整数x xx和p pp分别表示发生事故的区域编号和需要疏散的人数。输出格式输出一行包含若干个整数表示选择的疏散区域编号。如果有多个疏散区域到事故区域的最短路径长度相同则按照编号从小到大的顺序输出。样例输入4 -1 5 -1 8 5 -1 1 3 -1 1 -1 4 8 3 4 -1 10 20 15 25 2 12样例输出1数据范围2 ≤ n ≤ 10 4 2 \leq n \leq 10^42≤n≤104− 1 ≤ d i s t [ i ] [ j ] ≤ 1000 -1 \leq dist[i][j] \leq 1000−1≤dist[i][j]≤10001 ≤ c a p [ i ] ≤ 100 1 \leq cap[i] \leq 1001≤cap[i]≤1000 ≤ x n 0 \leq x n0≤xn0 p ≤ 1000 0 p \leq 10000p≤1000【题目解析】读取输入包括区域数量、区域之间的道路长度矩阵、每个区域的剩余容量、发生事故的区域编号以及需要疏散的人数。使用 Dijkstra 算法计算发生事故的区域到每个其他区域的最短路径长度。遍历所有区域对于每个区域检查其剩余容量是否大于等于需要疏散的人数并且计算到事故区域的最短路径长度。如果满足条件则将该区域加入备选疏散区域列表中。在备选疏散区域列表中选择路径长度最短的区域作为疏散目的地。输出选择的疏散区域编号。cpp#includeiostream#includevector#includequeue#includeclimitsusingnamespacestd;constintINFINT_MAX;// Structure to represent a city nodestructNode{intid;intcapacity;};// Structure to represent an edge between two nodesstructEdge{intto;intweight;};// Dijkstra algorithm to find shortest paths from a source node to all other nodesvectorintdijkstra(constvectorvectorEdgegraph,intsource){intngraph.size();vectorintdist(n,INF);priority_queuepairint,int,vectorpairint,int,greaterpairint,intpq;pq.push({0,source});dist[source]0;while(!pq.empty()){intupq.top().second;intdpq.top().first;pq.pop();if(ddist[u])continue;for(constEdgee:graph[u]){intve.to;intwe.weight;if(dist[u]wdist[v]){dist[v]dist[u]w;pq.push({dist[v],v});}}}returndist;}intmain(){intn;cinn;vectorvectorEdgegraph(n,vectorEdge(n));for(inti0;in;i){for(intj0;jn;j){intlength;cinlength;if(length!-1){graph[i].push_back({j,length});}}}vectorNodenodes(n);for(inti0;in;i){cinnodes[i].capacity;}intx,p;cinxp;x--;// Adjusting 1-based indexing to 0-based indexing// Find shortest paths from the accident nodevectorintshortest_pathsdijkstra(graph,x);// Find evacuation nodesvectorintevacuation_nodes;for(inti0;in;i){if(i!xnodes[i].capacitypshortest_paths[i]!INF){evacuation_nodes.push_back(i);}}// Sort evacuation nodes based on shortest pathssort(evacuation_nodes.begin(),evacuation_nodes.end(),[](inta,intb){returnshortest_paths[a]shortest_paths[b];});// Output selected evacuation nodesfor(inti0;ievacuation_nodes.size();i){coutevacuation_nodes[i]1 ;// Adjusting back to 1-based indexing}coutendl;return0;}整理试题不易你的关注是我更新的最大动力关注博主 带你看更多面试及竞赛试题和实用

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价