蓝桥杯真题:航班时间

 

 

输入输出样例

示例

输入

3
17:48:19 21:57:24
11:05:18 15:14:23
17:21:07 00:31:46 (+1)
23:02:41 16:13:20 (+1)
10:19:19 20:41:24
22:19:04 16:41:09 (+1)

输出

04:09:05
12:10:39
14:22:05

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 256M

 这题主要是两个问题:字符串处理和思路。

首先是思路,既然每组数据都是去的和回来的两个时间,我们可以忽略时差,为什么呢?因为抵消了。所以我们的时间就是两个时间的end-start相加之后除以二就是航班飞行时间了对吧。

之后就是字符串处理的问题:

比如我们读到降落时间的时候怎么去处理呢?我们可以这么做,首先和start时间一样用scanf读取三个时间参数,对后面的空格括号等等单独处理,用getchar处理,不过注意的是即使读完了第几天的数字也要继续读下去,因为下一次还得输入呐。

具体代码如下所示:

#include <bits/stdc++.h>
using namespace std;
const int day=24*60*60;
const int hour=60*60;
const int minute=60;
int start()
{
  int a,b,c;
  scanf("%d:%d:%d",&a,&b,&c);
  int time=a*hour+b*minute+c;
  return time;
}
int end()
{
  int a,b,c;
  scanf("%d:%d:%d",&a,&b,&c);
  int time=a*hour+b*minute+c;
  char ch,extra_day;
  while((ch=getchar())!='\n'&&ch!='\r')
  {
    if(ch=='(')
    {
      getchar();
      extra_day=getchar();
      time+=(extra_day-'0')*day;
    }
  }
  return time;
}
void display(int time)
{
  int a,b,c;
  a=time/hour;
  time%=hour;
  b=time/minute;
  c=time%minute;
  printf("%02d:%02d:%02d\n",a,b,c);
}

int main()
{
  // 请在此输入您的代码
  int t;
  scanf("%d",&t);
  while(t--)
  {
    int start1=start();
    int end1=end();
    int start2=start();
    int end2=end();
    int time=(end1-start1) + (end2-start2);
    display(time/2);
  }
  return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值