新浪微博上有人发了某老板的作息时间表,表示其每天 4:30 就起床了。但立刻有眼尖的网友问:这时间表不完整啊,早上九点到下午一点干啥了?
本题就请你编写程序,检查任意一张时间表,找出其中没写出来的时间段。
输入格式:
输入第一行给出一个正整数 N,为作息表上列出的时间段的个数。随后 N 行,每行给出一个时间段,格式为:
hh:mm:ss - hh:mm:ss
其中 hh、mm、ss 分别是两位数表示的小时、分钟、秒。第一个时间是开始时间,第二个是结束时间。题目保证所有时间都在一天之内(即从 00:00:00 到 23:59:59);每个区间间隔至少 1 秒;并且任意两个给出的时间区间最多只在一个端点有重合,没有区间重叠的情况。
输出格式:
按照时间顺序列出时间表中没有出现的区间,每个区间占一行,格式与输入相同。题目保证至少存在一个区间需要输出。
输入样例:
8
13:00:00 - 18:00:00
00:00:00 - 01:00:05
08:00:00 - 09:00:00
07:10:59 - 08:00:00
01:00:05 - 04:30:00
06:30:00 - 07:10:58
05:30:00 - 06:30:00
18:00:00 - 19:00:00
输出样例:
04:30:00 - 05:30:00
07:10:58 - 07:10:59
09:00:00 - 13:00:00
19:00:00 - 23:59:59
思路:用strcut去存一条时间的开始和结束,然后重写sort函数进行开始时间的升序排序,最后输出。注意要考虑num为0的情况,直接输出00:00:00 - 23:59:59,还有struct结构对象要足够大否则会段错误。
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
struct node{
string begin, end;
}times[100001];
bool cmp(const node& t1,const node& t2) {
return t1.begin < t2.begin;
}
int main()
{
int num;
cin >> num;
char c;
for (int i = 0; i < num; i++) {
cin >> times[i].begin >> c >> times[i].end;
}
sort(times, times + num, cmp);
if (times[0].begin != "00:00:00") {
if (num != 0) {
cout << "00:00:00" << " - " << times[0].begin << endl;
}
else {
cout << "00:00:00" << " - " << "23:59:59" << endl;
}
}
if (num > 1) {
for (int i = 0; i < num - 1; i++) {
if (times[i].end != times[i + 1].begin) {
cout << times[i].end << " - " << times[i + 1].begin << endl;
}
}
}
if (num >= 1) {
if (times[num - 1].end != "23:59:59") {
cout << times[num - 1].end << " - " << "23:59:59" << endl;
}
}
}