DataStructure小学期练习-老板的时间作息表

 1.用到结构体存储时间

struct node {
	string begin, end;
}times[100001];

2.用cmp函数和主函数中的sort来重装输入时间的排序,从小到大

bool cmp(const node& t1, const node& t2) {
	return t1.begin < t2.begin;
}//让结构体数据自动排序的函数

关于结构体数据排序:http://t.csdnimg.cn/aANuo

关于cmp:http://t.csdnimg.cn/FxLIq

3.因为第一段和最后一段的情况不能用结构体数据完全确定 所以单独处理

int main()
{
	int num;
	cin >> num;//输入n
	char c;
	for (int i = 0; i < num; i++) {
		cin >> times[i].begin >> c >> times[i].end;
	}//输入开始时间和结束时间
	sort(times, times + num, cmp);//sort函数排序
	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;

		}//单独判断num为0的情况,为0则固定输出,不为0则先输出第一个区间
	}
	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;
			}
		}//中间区间处理,循环条件num忽略0,从1开始(因为0已经单独处理过了),
//如果排序后的时间段前后不连接,则输出中间空缺
	}
	if (num >= 1) {
		if (times[num - 1].end != "23:59:59") {
			cout << times[num - 1].end << " - " << "23:59:59" << endl;

		}//单独处理最后一部分,如果结果不为时间段末尾,则单独加上一段空余部分
	}



}

代码参考:http://t.csdnimg.cn/W4Be0

哈哈忘记放题目了

命名空间:

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值