网易笔试 网易笔试题 0809
笔试时间:2025年8月9日
往年笔试合集:
第一题:部门旅游
今年小师妹将再次组织大家进行一次精彩的部门旅游。为了确保每位员工能够顺利参与,现有三个不同的旅游团可供选择。每个旅游团的人数限制各不相同,员工可根据自己的喜好选择报名的旅游团。为了公平起见,系统将采用先到先得的原则进行处理。报名规则如下:每位员工可以根据喜好报名参加1到3个旅游团。报名时,如果首选的旅游团已满员,系统将自动尝试加入员工的次选旅游团。如果员工所选择的所有旅游团均已满员,则该员工无法参加此次旅游。
输入描述
第一行包含一个整数 N,表示员工的数量 (1 ≤ N ≤ 10000)。
第二行包含三个整数,分别表示旅游团 A、B、C 的最大名额 (1 ≤ A,B,C ≤ 10000)。
接下来的 N 行,每行包含:
一个字符串,表示员工的 ID (由字母和数字组成,并且互不相同),ID 长度 <= 13。
一个整数 X (1 ≤ X ≤ 3),表示该员工选择的旅游团数量。
X 个字符 (A/B/C),表示员工选择的旅游团的优先级,优先级高的选择在前。
输出描述
按A、B、C三个团的顺序输出三行,每行一个整数,表示这个团最终的人数a,接下来是a个字符串,表示进入这个团的员工ID,请按照报名顺序输出。
样例输入
4
2 1 1
zhang01 2 A B
chen01 1 B
li02 3 B C A
yang 2 B A
样例输出
2 zhang01 yang
1 chen01
1 li02
参考题解
简单模拟
为每个旅游团维护一个列表,用于存储成功报名的员工ID,保证报名顺序。
依次处理每位员工的报名信息。
最后按A、B、C的顺序输出各团的人数及对应的员工ID。
C++:
#include <iostream> #include <vector> #include <string> using namespace std; int main() { int employeeCount; cin >> employeeCount; int maxA, maxB, maxC; cin >> maxA >> maxB >> maxC; vector<string> groupA, groupB, groupC; for (int i = 0; i < employeeCount; ++i) { string employeeID; int choiceCount; cin >> employeeID >> choiceCount; vector<char> choices(choiceCount); for (auto& choice : choices) { cin >> choice; } for (char choice : choices) { if (choice == 'A' && groupA.size() < maxA) { groupA.push_back(employeeID); break; } else if (choice == 'B' && groupB.size() < maxB) { groupB.push_back(employeeID); break; } else if (choice == 'C' && groupC.size() < maxC) { groupC.push_back(employeeID); break; } } } cout << groupA.size(); for (const string& id : groupA) { cout << " " << id; } cout << "\n"; cout << groupB.size(); for (const string& id : groupB) { cout << " " << id; } cout << "\n"; cout << groupC.size(); for (const string& id : groupC) { cout << " " << id; } cout << "\n"; }
Java:
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class EmployeeGrouping { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 读取员工数量 int employeeCount = scanner.nextInt(); // 读取各组最大容量 int maxA = scanner.nextInt(); int maxB = scanner.nextInt(); int maxC = scanner.nextInt(); // 初始化三个组 List<String> groupA = new ArrayList<>(); List<String> groupB = new ArrayList<>(); List<String> groupC = new ArrayList<>(); // 处理每个员工的分组 for (int i = 0; i < employeeCount; i++) { String employeeID = scanner.next(); int choiceCount = scanner.nextInt(); char[] choices = new char[choiceCount]; for (int j = 0; j < choiceCount; j++) { choices[j] = scanner.next().charAt(0); } // 按照选择优先级分配到第一个有空位的组 boolean assigned = false; for (char choice : choices) { if (choice == 'A' && groupA.size() < maxA) { groupA.add(employeeID); assigned = true; break; } else if (choice == 'B' && groupB.size() < maxB) { groupB.add(employeeID); assigned = true; break; } else if (choice == 'C' && groupC.size() < maxC) { groupC.add(employeeID); assigned = true; break; } } // 未分配成功的员工将被忽略 } // 输出各组结果 printGroup(groupA); printGroup(groupB); printGroup(groupC); scanner.close(); } // 打印组信息的辅助方法 private static void printGroup(List<String> group) { System.out.print(group.size()); for (String id : group) { System.out.print(" " + id); } System.out.println(); } }
Python:
# 读取员工数量 employee_count = int(input()) # 读取各组最大容量 max_a, max_b, max_c = map(int, input().split()) # 初始化三个组 group_a = [] group_b = [] group_c = [] # 处理每个员工的分组 for _ in range(employee_count): parts = input().split() employee_id = parts[0] choice_count = int(parts[1]) choices = parts[2:2+choice_count] # 按照选择优先级分配到第一个有空位的组 assigned = False for choice in choices: if choice == 'A' and len(group_a) < max_a: group_a.append(employee_id) assigned = True break elif choice == 'B' and len(group_b) < max_b: group_b.append(employee_id) assigned = True break elif choice == 'C' and len(group_c) < max_c: group_c.append(employee_id) assigned = True break # 未分配成功的员工将被忽略 # 定义打印组信息的函数 def print_group(group): print(len(group), end='') for id in group: print(f' {id}', end='') print() # 输出各组结果 print_group(group_a) print_group(group_b) print_group(group_c)
第二题:战力极差的最小值
在《无主星渊》的竞技宇宙中,来自各个星系的顶尖战队齐聚一堂,准备迎战传说中的最终BOSS。每个星系派出了最精锐的战队,共n支战队,每支战队有m名成员,每位成员的战力值分别为 P1, P2, … Pm。为了组成最强的终极挑战队,你需要从每支战队中各选1名成员(共n人),但团队配合至关重要。经过无数次模拟战斗,联盟科学家发现:战力越均衡的团队,越能激发协同共鸣。因此,选拔规则如下:在所有可能的组队方案中,选择战力极差(最大值-最小值)最小的方案,确保团队以最平衡的状态迎战BOSS。你的任务:计算所有可能的组队方案中,战力极差的最小值。
输入描述
第一行两个正整数 n (0 < n <= 3000)、m (0 < m <= 100),分别表示队伍数量与每只战队中的成员数量。
之后 n 行,每行输入 m 个数字 pi (0 < pi < 1e9),代表战队中成员的战力值。
输出描述
所有可能的组队方案中,战力极差的最小值。
样例输入
1 1
1
样例输出
0
参考题解
首先对每支战队的成员战力值进行排序,为后续的有序选择做准备使用一个多重集合来维护当前选择的成员战力值及所属战队初始时从每支战队选择战力值最小的成员加入集合每次记录当前集合中战力的最大值与最小值之差,更新最小极差移除集合中最小的战力值,并从其所属战队选择下一个战力值加入集合当某支战队的成员全部被考虑过时,结束循环最终得到的最小极差即为答案
C++:
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(0); cin.tie(0); int teamCount, memberCount; cin >> teamCount >> memberCount; vector<vector<ll>> powerValues(teamCount, vector<ll>(memberCount)); for (int i = 0; i < teamCount; ++i) { for (int j = 0; j < memberCount; ++j) { cin >> powerValues[i][j]; } sort(begin(powerValues[i]), end(powerValues[i])); } ll minDifference = 1LL << 60; vector<int> pointers(teamCount); multiset<pair<ll, int>> currentSelection; for (int i = 0; i < teamCount; ++i) { currentSelection.insert({ powerValues[i][0], i }); } while (true) { ll currentMin = currentSelection.begin()->first; ll currentMax = prev(currentSelection.end())->first; minDifference = min(minDifference, currentMax - currentMin); int teamIndex = currentSelection.begin()->second; currentSelection.erase(currentSelection.begin()); if (++pointers[teamIndex] == memberCount) break; currentSelection.insert({ powerValues[teamIndex][pointers[teamIndex]], teamIndex }); } cout << minDifference; }
Java:
import java.util.*; public class MinPowerDifference { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int teamCount = scanner.nextInt(); int memberCount = scanner.nextInt(); // 存储每个团队的能力值,并排序 List<List<Long>> powerValues = new ArrayList<>(); for (int i = 0; i < teamCount; i++) { List<Long> team = new ArrayList<>(); for (int j = 0; j < memberCount; j++) { team.add(scanner.nextLong()); } Collections.sort(team); powerValues.add(team); } long minDifference = (1L << 60); int[] pointers = new int[teamCount]; Arrays.fill(pointers, 0); // 使用TreeSet维护当前选择,按能力值排序,相同值按团队索引区分 TreeSet<long[]> currentSelection = new TreeSet<>((a, b) -> { if (a[0] != b[0]) { return Long.compare(a[0], b[0]); } return Long.compare(a[1], b[1]); // 确保相同值可以共存 }); // 初始化:每个团队选择第一个成员 for (int i = 0; i < teamCount; i++) { currentSelection.add(new long[]{powerValues.get(i).get(0), i}); } while (true) { long currentMin = currentSelection.first()[0]; long currentMax = currentSelection.last()[0]; minDifference = Math.min(minDifference, currentMax - currentMin); // 取出当前最小值所在的团队 long[] minElement = currentSelection.first(); int teamIndex = (int) minElement[1]; currentSelection.remove(minElement); // 移动该团队的指针 pointers[teamIndex]++; if (pointers[teamIndex] == memberCount) { break; // 某个团队已无更多成员可选,结束循环 } // 添加该团队的下一个成员 long nextValue = powerValues.get(teamIndex).get(pointers[teamIndex]); currentSelection.add(new long[]{nextValue, teamIndex}); } System.out.println(minDifference); scanner.close(); } }
Python:
import heapq def main(): import sys input = sys.stdin.read().split() ptr = 0 team_count = int(input[ptr]) ptr += 1 member_count = int(input[ptr]) ptr += 1 # 读取并排序每个团队的能力值 power_values = [] for _ in range(team_count): team = [] for _ in range(member_count): team.append(int(input[ptr])) ptr += 1 team.sort() power_values.append(team) min_difference = (1 << 60) pointers = [0] * team_count # 使用最小堆维护当前选择,同时跟踪最大值 heap = [] current_max = 0 # 初始化:每个团队选择第一个成员 for i in range(team_count): val = power_values[i][0] heapq.heappush(heap, (val, i)) if val > current_max: current_max = val while True: # 当前最小值 current_min, team_idx = heap[0] # 更新最小差值 min_difference = min(min_difference, current_max - current_min) # 移除当前最小值并移动对应团队的指针 heapq.heappop(heap) pointers[team_idx] += 1
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南