AcWing 1231. 航班时间

原题链接

考察:模拟

这道题如果我自己写会写得超繁琐....

思路:

        飞行时间 = 到达时间-出发时间+时差(时差由东向西为减,由西向东为加).可以发现将两次飞行的时间相加再/2就能得到飞行时间.

这题注意记录sscanf的用法,在某些地方非常好用.

注意getline使用前一定要检查是否前面有换行符.

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <string>
 5 using namespace std;
 6 const int N = 15;
 7 int Get_time()
 8 {
 9     string s;
10     getline(cin,s);
11     if(s.back()!=')') s+=" (+0)";
12     int h1,m1,s1,h2,m2,s2,d;
13     sscanf(s.c_str(),"%d:%d:%d %d:%d:%d (+%d)",&h1,&m1,&s1,&h2,&m2,&s2,&d);
14     int sec1 = h1*3600+m1*60+s1,sec2 = h2*3600+m2*60+s2;
15     return sec2-sec1+d*24*3600;
16 }
17 int main()
18 {
19     int T;
20     scanf("%d",&T);
21     getchar();
22     while(T--)
23     {
24         int t = Get_time()+Get_time()>>1;
25         int s = t%60,h = t/3600,m = t%3600/60;
26         printf("%02d:%02d:%02d\n",h,m,s);
27     }
28     return 0;
29 }

 

posted @ 2021-02-25 03:50  acmloser  阅读(60)  评论(0编辑  收藏  举报