L2-042 老板的作息表 sscanf() sprintf()

 输入格式: 

输入第一行给出一个正整数 N,为作息表上列出的时间段的个数。随后 N 行,每行给出一个时间段,格式为:

hh:mm:ss - hh:mm:ss

其中 hhmmss 分别是两位数表示的小时、分钟、秒。第一个时间是开始时间,第二个是结束时间。题目保证所有时间都在一天之内(即从 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

 Code:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 24 * 60 * 60 - 1;
int n ;
bool st[N];
int getsecond(char s[]){
    int hour , minute , second ;
    sscanf(s , "%02d:%02d:%02d" , &hour , &minute , &second);
    return hour * 3600 + minute * 60 + second ;
}

string formate(int x){
    int hour = x / 3600 ;
    x %= 3600 ;
    int minute = x / 60; 
    int second = x % 60 ;
    char c[10] ;
    sprintf(c , "%02d:%02d:%02d", hour , minute , second);
    return c;
}

int main()
{
    cin >> n ;
    while(n -- ){
        char a[10] , b[10] , c[10] ;
        scanf("%s%s%s", a, b , c);
        
        int l = getsecond(a) , r = getsecond(c);
        
        for(int i = l ; i < r ; i++ ) st[i] = true ;
    }
    for(int i = 0 , j = 0; i < N ; i++ ){
        if( !st[i]){
            j = i + 1;
            while(j < N && !st[j]) j++ ;
            cout << formate(i) << " - " << formate(j) << endl;
            i = j ;
        }
    }
    return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define l first
#define r second
typedef pair<string ,string> PSS ;

vector< PSS > v;
int n , m ;

int main() {
	scanf("%d", &n) ;
	v.push_back({"00:00:00", "00:00:00"});//起始时间
    
	for (int i = 0; i < n; i++) {
		string a,b,c;
		cin >> a >> b >> c;
		v.push_back({a, c});
	}
	v.push_back({"23:59:59", "23:59:59"});//结束时间

	sort(v.begin(), v.end());
	for (int i = 1; i <= n + 1; i++) {
		if(v[i - 1].r < v[i].l) { 
			cout << v[i - 1].r << " - " << v[i].l << endl;
		}
	}
	
 	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值