华为笔试 华为笔试题 0424
笔试时间:2025年04月24日
历史笔试传送门:
第一题
题目:物料回收计算器
某云站点有一批服务器待下线,已知这些服务器所对应的物料编码(代表不同服务器型号的标识字符串),特定物料编码本身已包含CPU/内存/主板等备件型号,请计算出这批服务器下线后可回收不同的CPU/内存/主板型号和数量。物料编码只包含大写英文字母和数字,其中一定包含三类备件型号,它的固定格式是一个大写字母+两个数字,字母C代表CPU,M代表内存Memory,B代表主板Board。其他字母和数字可忽略,同类备件存在多个型号时取第一个。
输入描述
第一行为服务器个数N,范围是[1,1000]:
第二行为N个物料编码字符串,依次用空格隔开
输出描述
统一按照型号1,双量1,型号2,数量2,型号3,数量3.的格式输出
第一行为CPU的型号和对应数量
第二行为内存的型号和对应数量
第三行为支板的型号和对应数量
同类备件存在多个型号时按照字典序输出,分号隔开
样例输入
2
C01M23B050130 C01M23B060130
样例输出
C01,2
M23,2
B05,1;B06,1
参考题解
直接模拟,注意字符串拆分,以及对同类备件多次出现的时候特殊处理一下即可。
C++:[此代码未进行大量数据的测试,仅供参考]
#include <bits/stdc++.h> using namespace std; // Parse out first C##, M##, B## segments tuple<string,string,string> parse_code(const string &code) { string cpu="", memory="", board=""; int L = code.size(); for(int i = 0; i + 2 < L; ++i) { char t = code[i]; if (t=='C' && isdigit(code[i+1]) && isdigit(code[i+2])) { if (cpu.empty()) cpu = code.substr(i,3); } else if (t=='M' && isdigit(code[i+1]) && isdigit(code[i+2])) { if (memory.empty()) memory = code.substr(i,3); } else if (t=='B' && isdigit(code[i+1]) && isdigit(code[i+2])) { if (board.empty()) board = code.substr(i,3); } } return {cpu, memory, board}; } // Count occurrences across all codes void count_spares(const vector<string> &codes, map<string,int> &cpu_cnt, map<string,int> &mem_cnt, map<string,int> &board_cnt) { for (auto &c : codes) { auto [cpu, mem, brd] = parse_code(c); if (!cpu.empty()) cpu_cnt[cpu]++; if (!mem.empty()) mem_cnt[mem]++; if (!brd.empty()) board_cnt[brd]++; } } // Format map as "key,value;key,value;..." string format_output(const map<string,int> &cnt) { string out; bool first = true; for (auto &kv : cnt) { if (!first) out += ";"; out += kv.first + "," + to_string(kv.second); first = false; } return out; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<string> codes(N); for (int i = 0; i < N; ++i) { cin >> codes[i]; } map<string,int> cpu_cnt, mem_cnt, board_cnt; count_spares(codes, cpu_cnt, mem_cnt, board_cnt); cout << format_output(cpu_cnt) << "\n"; cout << format_output(mem_cnt) << "\n"; cout << format_output(board_cnt) << "\n"; return 0; }
Java:[此代码未进行大量数据的测试,仅供参考]
import java.util.*; public class SpareCounter { // Parse out first C##, M##, B## segments public static String[] parseCode(String code) { String cpu = "", mem = "", board = ""; int L = code.length(); for (int i = 0; i + 2 < L; i++) { char t = code.charAt(i); if (t == 'C' && Character.isDigit(code.charAt(i+1)) && Character.isDigit(code.charAt(i+2))) { if (cpu.isEmpty()) cpu = code.substring(i, i+3); } else if (t == 'M' && Character.isDigit(code.charAt(i+1)) && Character.isDigit(code.charAt(i+2))) { if (mem.isEmpty()) mem = code.substring(i, i+3); } else if (t == 'B' && Character.isDigit(code.charAt(i+1)) && Character.isDigit(code.charAt(i+2))) { if (board.isEmpty()) board = code.substring(i, i+3); } } return new String[]{cpu, mem, board}; } // Count occurrences across all codes public static void countSpares(List<String> codes, Map<String,Integer> cpuCnt, Map<String,Integer> memCnt, Map<String,Integer> boardCnt) { for (String c : codes) { String[] parts = parseCode(c); if (!parts[0].isEmpty()) cpuCnt.put(parts[0], cpuCnt.getOrDefault(parts[0],0) + 1); if (!parts[1].isEmpty()) memCnt.put(parts[1], memCnt.getOrDefault(parts[1],0) + 1); if (!parts[2].isEmpty()) boardCnt.put(parts[2], boardCnt.getOrDefault(parts[2],0) + 1); } } // Format map as "key,value;key,value;..." public static String formatOutput(Map<String,Integer> cnt) { StringBuilder sb = new StringBuilder(); List<String> keys = new ArrayList<>(cnt.keySet()); Collections.sort(keys); for (int i = 0; i < keys.size(); i++) { if (i > 0) sb.append(";"); String k = keys.get(i); sb.append(k).append(",").append(cnt.get(k)); } return sb.toString(); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); List<String> codes = new ArrayList<>(); for (int i = 0; i < N; i++) { codes.add(sc.next()); } Map<String,Integer> cpuCnt = new HashMap<>(); Map<String,Integer> memCnt = new HashMap<>(); Map<String,Integer> boardCnt = new HashMap<>(); countSpares(codes, cpuCnt, memCnt, boardCnt); System.out.println(formatOutput(new TreeMap<>(cpuCnt))); System.out.println(formatOutput(new TreeMap<>(memCnt))); System.out.println(formatOutput(new TreeMap<>(boardCnt))); sc.close(); } }
Python:[此代码未进行大量数据的测试,仅供参考]
# 解析物料编码 def parse_code(code): cpu = None memory = None board = None for i in range(len(code) - 2): if code[i] == 'C'and code[i + 1].isdigit() and code[i + 2].isdigit(): if cpu isNone: cpu = code[i:i + 3] elif code[i] == 'M'and code[i + 1].isdigit() and code[i + 2].isdigit(): if memory isNone: memory = code[i:i + 3] elif code[i] == 'B'and code[i + 1].isdigit() and code[i + 2].isdigit(): if board isNone: board = code[i:i + 3] return cpu, memory, board # 统计备件数量 def count_spares(codes): cpu_count = {} memory_count = {} board_count = {} for code in codes: cpu, memory, board = parse_code(code) if cpu: cpu_count[cpu] = cpu_count.get(cpu, 0) + 1 if memory: memory_count[memory] = memory_count.get(memory, 0) + 1 if board: board_count[board] = board_count.get(board, 0) + 1 return cpu_count, memory_count, board_count # 格式化输出 def format_output(count_dict): items = sorted(count_dict.items()) return';'.join([f'{key},{value}'for key, value in items]) N = int(input()) codes = input().split() cpu_count, memory_count, board_count = count_spares(codes) print(format_output(cpu_count)) print(format_output(memory_c
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
2025 春招笔试合集 文章被收录于专栏
2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南