任何疑问、意见、建议请留言公众号:一航代码
题目描述:
输入N个员工,每个员工输出ID号,上班时间,下班时间,第一行输出最早去的员工的ID和上班时间第二行输出最迟走的员工的ID和下班时间第三行输出工作最久的员工的ID和上班时间
输入格式:
第一行输入N表示有N个员工,在接下来的N行,依次输入员工ID,上班打卡时间,下班打卡时间。
输出格式:
在接下来的三行依次输出最早来的员工ID,上班时间。最迟走的员工ID,下班时间。工作时间最久的员工ID和上班时长。
输入样例:
ID100001 07:00:00 17:00:00
ID100002 08:00:00 18:00:00
ID100003 09:00:00 21:00:00
输出样例:
ID100001 07:00:00
ID100003 21:00:00
ID100003 12:00:00
解决方法:
(1)代码实现:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
struct person
{
string id = "";
int ts = 0, te = 0;
int worktime;
};
bool cmps(person a, person b) //最早上班
{
return a.ts < b.ts;
}
bool cmpe(person a, person b) //最晚下班
{
return a.te > b.te;
}
bool cmpl(person a, person b) //上班时间最久
{
return a.worktime > b.worktime;
}
int main()
{
int n;
cin >> n;
vector<person> p;
for (int i = 0; i < n; i++)
{
person t;
int tsh, tsm, tss, teh, tem, tes;
cin >> t.id;
scanf("%d:%d:%d %d:%d:%d", &tsh, &tsm, &tss, &teh, &tem, &tes);
t.ts = tsh * 3600 + tsm * 60 + tss; //统一尺度
t.te = teh * 3600 + tem * 60 + tes;
t.worktime = t.te - t.ts;
p.push_back(t);
}
sort(p.begin(), p.end(), cmps);
cout << p[0].id;
printf(" %02d:%02d:%02d\n", p[0].ts / 3600, (p[0].ts % 3600) / 60, (p[0].ts % 3600) % 60);
sort(p.begin(), p.end(), cmpe);
cout << p[0].id;
printf(" %02d:%02d:%02d\n", p[0].te / 3600, (p[0].te % 3600) / 60, (p[0].te % 3600) % 60);
sort(p.begin(), p.end(), cmpl);
cout << p[0].id;
printf(" %02d:%02d:%02d\n", p[0].worktime / 3600, (p[0].worktime % 3600) / 60, (p[0].worktime % 3600) % 60);
system("pause");
return 0;
}