#C0E80. 华为OD机试E卷 - 优秀学员统计
华为OD机试E卷 - 优秀学员统计
华为OD机试E卷 - 优秀学员统计(Java & Python& JS & C++ & C )
https://blog.csdn.net/banxia_frontend/article/details/142730119
最新华为OD机试
题目描述
公司某部门软件教导团正在组织新员工每日打卡学习活动,他们开展这项学习活动已经一个月了,所以想统计下这个月优秀的打卡员工。每个员工会对应一个id,每天的打卡记录记录当天打卡员工的id集合,一共30天。
请你实现代码帮助统计出打卡次数top5的员工。加入打卡次数相同,将较早参与打卡的员工排在前面,如果开始参与打卡的时间还是一样,将id较小的员工排在前面。
注:不考虑并列的情况,按规则返回前5名员工的id即可,如果当月打卡的员工少于5个,按规则排序返回所有有打卡记录的员工id。
输入描述
第一行输入为新员工数量N,表示新员工编号id为0到N-1,N的范围为[1,100]
第二行输入为30个整数,表示每天打卡的员工数量,每天至少有1名员工打卡。
之后30行为每天打卡的员工id集合,id不会重复。
输出描述
按顺序输出打卡top5员工的id,用空格隔开。
示例1
输入
11
4 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2
0 1 7 10
0 1 6 10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
6 10
7 10
输出
10 0 1 7 6
说明
员工编号范围为0~10,id为10的员工连续打卡30天,排第一,id为0,1,6,7的员工打卡都是两天,id为0,1,7的员工在第一天就打卡,比id为6的员工早,排在前面,0,1,7按id升序排列,所以输出[10,0,1,7,6]
示例2
输入
7
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
输出
0 1 2 3 4
说明
员工编号范围为0-6,id为0,1,2,3,4,5的员工打卡次数相同,最早开始打卡的时间也一样,所以按id升序返回前5个id
示例3
输入
2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0 1
0 1
输出
1 0
说明
只有两名员工参与打卡,按规则排序输出两名员工的id
解题思路
典型的排序类问题。先按照打卡次数,再按照打卡时间先后进行排序即可。
Java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringJoiner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int monthCount = scanner.nextInt(); // 打卡月份数
int[] dayCount = new int[30]; // 每个月的天数
for (int i = 0; i < 30; i++) {
dayCount[i] = scanner.nextInt();
}
int[][] dayIds = new int[30][]; // 每天打卡员工的id
for (int i = 0; i < 30; i++) {
int m = dayCount[i];
dayIds[i] = new int[m];
for (int j = 0; j < m; j++) {
dayIds[i][j] = scanner.nextInt();
}
}
System.out.println(getResult(dayIds));
}
public static String getResult(int[][] dayIds) {
HashMap<Integer, Integer[]> employees = new HashMap<>(); // 员工id和打卡信息的映射
for (int i = 0; i < dayIds.length; i++) {
int[] ids = dayIds[i];
for (int id : ids) {
if (employees.containsKey(id)) {
employees.get(id)[0]++; // 打卡次数+1
} else {
// 加入数组含义是:该id员工的 [打卡次数,第一天打卡日期]
employees.put(id, new Integer[] {1, i});
}
}
}
ArrayList<Integer[]> list = new ArrayList<>(); // 存储有打卡记录的员工信息
for (Integer id : employees.keySet()) {
Integer[] employee = employees.get(id);
int count = employee[0]; // 打卡次数
int firstDay = employee[1]; // 第一天打卡日期
list.add(new Integer[] {id, count, firstDay});
}
// 按规则排序
list.sort(
(a, b) ->
a[1].equals(b[1]) ? (a[2].equals(b[2]) ? a[0] - b[0] : a[2] - b[2]) : b[1] - a[1]);
StringJoiner sj = new StringJoiner(" ");
// 不考虑并列的情况,按规则返回前5名员工的id即可,如果当月打卡的员工少于5个,按规则排序返回所有有打卡记录的员工id
for (int i = 0; i < Math.min(5, list.size()); i++) {
sj.add(list.get(i)[0] + "");
}
return sj.toString();
}
}
Python
def get_result(day_ids):
employees = {} # 员工id和打卡信息的映射 {id: [打卡次数, 第一天打卡日期]}
for i, ids in enumerate(day_ids):
for emp_id in ids:
if emp_id in employees:
employees[emp_id][0] += 1 # 打卡次数+1
else:
employees[emp_id] = [1, i] # 初始化打卡次数和第一次打卡日期
# 存储有打卡记录的员工信息
employee_list = [[emp_id, info[0], info[1]] for emp_id, info in employees.items()]
# 按规则排序
employee_list.sort(key=lambda x: (-x[1], x[2], x[0])) # 打卡次数降序, 第一次打卡日期升序, 员工id升序
# 不考虑并列情况,按规则返回前5名员工的id,如果不足5个则返回所有
result = [str(employee[0]) for employee in employee_list[:5]]
return " ".join(result)
if __name__ == "__main__":
month_count = int(input()) # 打卡月份数
# 每个月的天数
day_count = list(map(int, input().split()))
# 每天打卡员工的id
day_ids = []
for i in range(30):
day_ids.append(list(map(int, input().split())))
# 输出结果
print(get_result(day_ids))
JavaScript
// 引入 Node.js 内置的 readline 模块
const readline = require("readline");
// 创建 readline.Interface 实例
const rl = readline.createInterface({
input: process.stdin, // 从标准输入流中读取数据
output: process.stdout, // 向标准输出流中输出数据
});
// 用于存储输入的所有行
const inputLines = [];
// 当读取到新的一行时,将其添加到 inputLines 数组中
rl.on("line", (line) => {
inputLines.push(line);
// 当输入的行数达到 32 时,进行处理并输出结果
if (inputLines.length === 32) {
// 获取员工总数
const employeeCount = Number(inputLines[0]);
// 获取每个员工出现的天数
const dayCount = inputLines[1].split(" ").map(Number);
// 获取每个员工在每天出现的次数和日期
const dayIds = inputLines.slice(2).map((line) => line.split(" ").map(Number));
// 输出出现次数最多的前五个员工的 ID
console.log(getTopEmployeeIds(dayIds));
// 清空 inputLines 数组,以便下一次读取数据
inputLines.length = 0;
}
});
// 获取出现次数最多的前五个员工的 ID
function getTopEmployeeIds(dayIds) {
// 用一个对象来存储每个员工的出现次数和首次出现的日期
const employees = {};
// 遍历 dayIds 数组,统计每个员工的出现次数和首次出现的日期
for (let i = 0; i < dayIds.length; i++) {
const ids = dayIds[i];
for (let id of ids) {
if (employees[id]) {
employees[id].count++;
} else {
employees[id] = {
count: 1,
firstDay: i,
};
}
}
}
// 将 employees 对象转化为数组,并按照出现次数、首次出现日期、ID 的顺序排序
let arr = [];
for (let id in employees) {
const { count, firstDay } = employees[id];
arr.push([id, count, firstDay]);
}
arr.sort((a, b) =>
b[1] !== a[1] ? b[1] - a[1] : b[2] !== a[2] ? a[2] - b[2] : a[0] - b[0]
);
// 取出前五个员工的 ID,并以空格分隔返回
return arr
.slice(0, 5)
.map(([id]) => id)
.join(" ");
}
C++
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
// 获取结果函数
string getResult(const vector<vector<int>>& dayIds) {
unordered_map<int, pair<int, int>> employees; // 员工id和打卡信息的映射:{id -> {打卡次数, 第一天打卡日期}}
for (int i = 0; i < dayIds.size(); i++) {
for (int id : dayIds[i]) {
if (employees.find(id) != employees.end()) {
employees[id].first++; // 打卡次数+1
} else {
employees[id] = {1, i}; // 第一次打卡日期
}
}
}
vector<tuple<int, int, int>> list; // 存储有打卡记录的员工信息:{id, 打卡次数, 第一天打卡日期}
for (const auto& entry : employees) {
list.emplace_back(entry.first, entry.second.first, entry.second.second);
}
// 按规则排序
sort(list.begin(), list.end(), [](const tuple<int, int, int>& a, const tuple<int, int, int>& b) {
if (get<1>(a) != get<1>(b)) {
return get<1>(a) > get<1>(b); // 按打卡次数降序
}
if (get<2>(a) != get<2>(b)) {
return get<2>(a) < get<2>(b); // 按第一次打卡日期升序
}
return get<0>(a) < get<0>(b); // 按员工id升序
});
// 返回前5名员工的id(或不足5名时返回所有)
stringstream result;
for (int i = 0; i < min(5, (int)list.size()); i++) {
if (i > 0) {
result << " ";
}
result << get<0>(list[i]);
}
return result.str();
}
int main() {
int monthCount; // 打卡月份数
cin >> monthCount;
vector<int> dayCount(30); // 每个月的天数
for (int i = 0; i < 30; i++) {
cin >> dayCount[i];
}
vector<vector<int>> dayIds(30); // 每天打卡员工的id
for (int i = 0; i < 30; i++) {
int m = dayCount[i];
dayIds[i].resize(m);
for (int j = 0; j < m; j++) {
cin >> dayIds[i][j];
}
}
// 输出结果
cout << getResult(dayIds) << endl;
return 0;
}
C语言
#include <stdio.h>
#include <stdlib.h>
#define MAX_EMPLOYEES 100
#define DAYS 30
// 结构体存储员工信息
typedef struct {
int id;
int count; // 打卡次数
int first_day; // 首次打卡的天数
} EmployeeInfo;
// 根据规则排序的比较函数
int compare(const void *a, const void *b) {
EmployeeInfo *empA = (EmployeeInfo *)a;
EmployeeInfo *empB = (EmployeeInfo *)b;
if (empA->count == empB->count) {
if (empA->first_day == empB->first_day) {
return empA->id - empB->id; // ID小的员工排前
} else {
return empA->first_day - empB->first_day; // 首次打卡早的排前
}
} else {
return empB->count - empA->count; // 打卡次数多的排前
}
}
int main() {
int n; // 新员工数量
scanf("%d", &n);
int employeeCount[DAYS]; // 每天打卡的员工数量
for (int i = 0; i < DAYS; i++) {
scanf("%d", &employeeCount[i]);
}
int employeeIds[DAYS][MAX_EMPLOYEES]; // 每天打卡的员工ID
for (int i = 0; i < DAYS; i++) {
for (int j = 0; j < employeeCount[i]; j++) {
scanf("%d", &employeeIds[i][j]);
}
}
EmployeeInfo employees[MAX_EMPLOYEES]; // 存储每个员工的信息
int isTracked[MAX_EMPLOYEES] = {0}; // 标记是否已记录该员工
int employeeIndex = 0;
// 统计打卡信息
for (int i = 0; i < DAYS; i++) {
for (int j = 0; j < employeeCount[i]; j++) {
int id = employeeIds[i][j];
if (isTracked[id]) {
// 如果员工已经记录过,增加打卡次数
for (int k = 0; k < employeeIndex; k++) {
if (employees[k].id == id) {
employees[k].count++;
break;
}
}
} else {
// 如果员工没有记录过,新增记录
employees[employeeIndex].id = id;
employees[employeeIndex].count = 1;
employees[employeeIndex].first_day = i;
isTracked[id] = 1;
employeeIndex++;
}
}
}
// 按照规则排序
qsort(employees, employeeIndex, sizeof(EmployeeInfo), compare);
// 输出前5名员工的ID
for (int i = 0; i < employeeIndex && i < 5; i++) {
printf("%d", employees[i].id);
if (i != 4 && i != employeeIndex - 1) {
printf(" ");
}
}
printf("\n");
return 0;
}
完整用例
用例1
11
4 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2
0 1 7 10
0 1 6 10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
6 10
7 10
用例2
7
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
用例3
2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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