华为机试(C++)真题OD
时间: 2025-02-01 07:54:15 浏览: 35
### 华为机试 C++ 真题及答案解析
#### 考勤信息题目描述
在一个公司里,员工每天上下班都需要打卡记录考勤情况。现在给定一段时间内的所有打卡记录,请统计每位员工在这段时间内迟到、早退和缺勤的情况。
具体来说:
- 迟到定义为上班打卡时间晚于规定上班时间。
- 早退定义为下班打卡时间早于规定下班时间。
- 缺勤定义为当天没有任何打卡记录。
输入数据包含多组测试用例,每组测试用例的第一行为三个整数N, M, K分别表示总天数、规定的上班时间和规定的下班时间;接下来N行,每一行先给出一个字符串S代表员工姓名,再跟两个整数A,B,其中A=-1表示该员工这天未上班打卡,B=-1表示该员工这天未下班打卡,其他情况下这两个数值则分别为实际的上班和下班时刻。
输出对于每个测试案例的结果应按照如下格式打印:依次列出所有不同名字的员工以及他们对应的迟到了多少次、早退了多少次还有缺席了多少整天[^1]。
```cpp
#include <iostream>
#include <map>
#include <vector>
using namespace std;
struct Record {
string name;
int arriveTime;
int leaveTime;
};
void processAttendance(vector<Record>& records, const map<string, pair<int, int>>& workHours) {
map<string, vector<int>> stats; // {name -> [lateCount, earlyLeaveCount, absentCount]}
for (const auto& record : records) {
if (!stats.count(record.name)) {
stats[record.name] = {0, 0, 0};
}
bool isAbsent = false;
if (record.arriveTime == -1 || record.leaveTime == -1) {
++(stats[record.name][2]);
isAbsent = true;
} else {
if (record.arriveTime > workHours.at(record.name).first && !isAbsent) {
++(stats[record.name][0]);
}
if (record.leaveTime < workHours.at(record.name).second && !isAbsent) {
++(stats[record.name][1]);
}
}
}
for (auto &[name, stat] : stats) {
cout << "Employee Name: " << name
<< ", Late Count: " << stat[0]
<< ", Early Leave Count: " << stat[1]
<< ", Absent Days: " << stat[2] << endl;
}
}
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<Record> attendanceRecords(N);
map<string, pair<int, int>> standardWorkTimes;
while (cin >> N >> M >> K) {
for (int i = 0; i < N; ++i) {
cin >> attendanceRecords[i].name
>> attendanceRecords[i].arriveTime
>> attendanceRecords[i].leaveTime;
if (!standardWorkTimes.contains(attendanceRecords[i].name))
standardWorkTimes.insert({attendanceRecords[i].name, make_pair(M, K)});
}
processAttendance(attendanceRecords, standardWorkTimes);
}
}
```
此代码实现了对考勤系统的模拟处理,并能够计算出每一位员工的具体违规次数。需要注意的是,在真实环境中应当考虑更多边界条件和其他可能影响结果的因素[^2]。
阅读全文
相关推荐



















