learn && wrong
1、early没有更新,不知道为什么
b.hh初试是24呀,但是显示的是00,局部作用域的原因?这题很容易错,不是void()居然能过,应该是init(),没有初始化居然是0
2、我总觉得两个比较函数很有问题,有逻辑错误,但是样例全对了,
person less1(person a, person b) { //找出最早那个,小于
if (a.hh < b.hh) return a; //按照这个逻辑,如果a.hh没有小于b.hh,即a.hh > b.hh,但是a.mm < b.mm,就return a了,比如temp是10:05:00,而early是07:06:00,所以我要怎么改,怎么判断谁大谁小
else if (a.mm < b.mm) return a;
else if (a.ss < b.ss) return a;
else return b;
}
标准答案是,只写了一个比较大函数,比较简洁,而且返回布尔值,根据布尔值来判断是否复制,而不是跟我一样返回结构体,比较省空间,其他手法一样
/*
编程思想:
1、结构体吗,如何比较时间呢,记得是比较字符串大小,不是比较字符串大小,用greater函数,比较输入输出谁大,暂存即可
*/
#include <iostream>
#include <algorithm>
using namespace std;
struct person {
char id[16];
int hh;
int mm;
int ss;
}temp, ans_latest, ans_early;
person greater1(person a, person b) { //找出最晚的那个,逻辑错了
if (a.hh > b.hh) return a;
else if (a.mm > b.mm) return a;
else if (a.ss > b.ss) return a;
else return b;
}
person less1(person a, person b) { //找出最早那个,小于
if (a.hh < b.hh) return a; //按照这个逻辑,如果a.hh没有小于b.hh,即a.hh > b.hh,但是a.mm < b.mm,就return a了,比如temp是10:05:00,而early是07:06:00,所以我要怎么改,怎么判断谁大谁小
else if (a.mm < b.mm) return a;
else if (a.ss < b.ss) return a;
else return b;
}
void init() { //初始化最大最小
ans_early.hh = 24;ans_early.mm = ans_early.ss = 0;
ans_latest.hh = ans_latest.mm = ans_latest.ss = 0;
}
int main()
{
init();//初始化
int n;
cin >> n;//总共多少个员工
for (int i = 0;i < n;i++) {
cin >> temp.id;
scanf("%d:%d:%d", &temp.hh, &temp.mm, &temp.ss); //读入上班时间
ans_early = less1(temp, ans_early); //比较最早上班时间才对呀,
scanf("%d:%d:%d", &temp.hh, &temp.mm, &temp.ss); //读入下班时间
ans_latest = greater1(temp, ans_latest); //比较最晚下班时间
}
cout << ans_early.id << " " << ans_latest.id;
return 0;
}