- //#include <iostream>
- //#include <vector>
- //#include <map>
- //#include <algorithm>
- #include <bits/stdc++.h>
- using namespace std;
- struct Employee {
- string e_id, e_name, d_id;
- int e_salary;
- };
- struct Project {
- string p_id, p_name, p_location;
- };
- struct HourLog {
- string e_id, p_id;
- int p_hours;
- };
- int main() {
- int E, D, P, H;
- cin >> E >> D >> P >> H;
- cin.ignore();
- vector<Employee> employees(E);
- for (int i = 0; i < E; i++) {
- cin >> employees[i].e_id >> employees[i].e_name >> employees[i].e_salary >> employees[i].d_id;
- }
- map<string, string> project_names;
- for (int i = 0; i < D; i++) {
- string d_id, d_name, m_id;
- cin >> d_id >> d_name >> m_id;
- }
-
- for (int i = 0; i < P; i++) {
- string p_id, p_name, p_location;
- cin >> p_id >> p_name >> p_location;
- project_names[p_id] = p_name;
- }
-
- vector<HourLog> hour_logs(H);
- for (int i = 0; i < H; i++) {
- cin >> hour_logs[i].e_id >> hour_logs[i].p_id >> hour_logs[i].p_hours;
- }
-
- string query_name;
- cin >> query_name;
-
- map<string, int> project_hours;
- for (const auto &emp : employees) {
- if (emp.e_name == query_name) {
- for (const auto &log : hour_logs) {
- if (log.e_id == emp.e_id) {
- project_hours[project_names[log.p_id]] += log.p_hours;
- }
- }
- break;
- }
- }
-
- int total_hours = 0;
- vector<pair<string, int>> sorted_projects(project_hours.begin(), project_hours.end());
- sort(sorted_projects.begin(), sorted_projects.end());
-
- for (const auto &entry : sorted_projects) {
- cout << entry.first << " " << entry.second << endl;
- total_hours += entry.second;
- }
- cout << total_hours << endl;
-
- return 0;
- }
複製代碼 回復 1# may
----------------------------------------------
程式說明:
資料結構設計
Employee 結構體儲存員工資訊。
Project 結構體儲存專案資訊。
HourLog 結構體儲存員工參與專案時數。
資料輸入
依序讀取 EMPLOYEE、DEPARTMENT、PROJECT、HOUR_LOG 四類資料。
透過 map 儲存 PROJECT 資料,使專案編號對應專案名稱。
資料處理
透過 query_name 找到對應 e_id,再比對 HOUR_LOG 記錄。
利用 map 來累計同一專案的時數。
輸出處理
map 轉換為 vector<pair<string, int>> 以專案名稱排序輸出。
最後輸出總時數。
---------------------------------
測資:
測試資料00
輸入
3 2 2 4
E001 John 45000 D001
E002 Mary 43000 D002
E003 Tom 46000 D002
D001 RD E001
D002 SALE E002
P001 AI Taipei
P002 SE Tainan
E001 P001 20
E002 P002 30
E003 P001 20
E002 P001 10
Mary
輸出
AI 10
SE 30
40
測試資料 01
輸入
4 3 4 7
E001 John 45000 D001
E002 Mary 43000 D002
E003 Tom 46000 D002
E004 Jame 50000 D003
D001 RD E001
D002 SALE E002
D003 Product E004
P001 AI Taipei
P002 SE Tainan
P003 IMG UK
P004 WEB Taipei
E001 P001 20
E004 P004 20
E002 P002 30
E004 P002 20
E003 P001 20
E002 P001 10
E004 P003 20
Jame
輸出
IMG 20
SE 20
WEB 20
60
測試資料 02
輸入
2 1 3 3
E001 Alice 50000 D001
E002 Bob 55000 D001
D001 HR E001
P001 Cloud NewYork
P002 DevOps SF
P003 Security London
E001 P001 15
E001 P002 10
E002 P003 20
Alice
輸出
Cloud 15
DevOps 10
25
測試資料 03
輸入
5 2 3 6
E001 John 40000 D001
E002 Mary 45000 D002
E003 David 48000 D001
E004 Kevin 42000 D002
E005 Emma 47000 D001
D001 IT E001
D002 HR E002
P001 ML Tokyo
P002 Data Taipei
P003 AI London
E002 P001 25
E004 P002 30
E005 P003 15
E003 P002 10
E001 P001 20
E002 P003 20
Kevin
輸出
Data 30
30
測試資料 04
輸入
3 2 3 5
E001 Alex 55000 D001
E002 Brian 53000 D002
E003 Chloe 56000 D001
D001 Finance E001
D002 Marketing E002
P001 UX Berlin
P002 Backend Paris
P003 AI Zurich
E001 P001 30
E002 P002 40
E003 P003 25
E002 P001 15
E001 P003 20
Brian
輸出
Backend 40
UX 15
55
這些測試資料涵蓋了:
員工參與不同數量的專案
有些員工未參與專案
員工參與多個專案
員工參與時數累計並按專案名稱排序 |