蓝桥杯 航班时间 (字符串处理)


标题:航班时间

【问题背景】
小h前往美国参加了蓝桥杯国际赛。小h的女朋友发现小h上午十点出发,上午十二点到达美国,于是感叹到“现在飞机飞得真快,两小时就能到美国了”。

小h对超音速飞行感到十分恐惧。仔细观察后发现飞机的起降时间都是当地时间。由于北京和美国东部有12小时时差,故飞机总共需要14小时的飞行时间。

不久后小h的女朋友去中东交换。小h并不知道中东与北京的时差。但是小h得到了女朋友来回航班的起降时间。小h想知道女朋友的航班飞行时间是多少。

【问题描述】
对于一个可能跨时区的航班,给定来回程的起降时间。假设飞机来回飞行时间相同,求飞机的飞行时间。

【输入格式】
从标准输入读入数据。
一个输入包含多组数据。

输入第一行为一个正整数T,表示输入数据组数。
每组数据包含两行,第一行为去程的 起降 时间,第二行为回程的 起降 时间。
起降时间的格式如下

h1:m1:s1 h2:m2:s2

h1:m1:s1 h3:m3:s3 (+1)

h1:m1:s1 h4:m4:s4 (+2)
表示该航班在当地时间h1时m1分s1秒起飞,

第一种格式表示在当地时间 当日 h2时m2分s2秒降落
第二种格式表示在当地时间 次日 h3时m3分s3秒降落。
第三种格式表示在当地时间 第三天 h4时m4分s4秒降落。

对于此题目中的所有以 h:m:s 形式给出的时间, 保证 ( 0<=h<=23, 0<=m,s<=59 ).

【输出格式】
输出到标准输出。

对于每一组数据输出一行一个时间hh:mm:ss,表示飞行时间为hh小时mm分ss秒。
注意,当时间为一位数时,要补齐前导零。如三小时四分五秒应写为03:04:05。

【样例输入】
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

【限制与约定】
保证输入时间合法,飞行时间不超过24小时。


资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

注意:
main函数需要返回0;
只使用ANSI C/ANSI C++ 标准;
不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include <xxx>
不能通过工程设置而省略常用头文件。

提交程序时,注意选择所期望的语言类型和编译器类型。

思路:使用getline输入每一行时间保存为string,然后利用string的下标获取数字,然后转化为每个时间。

注意:cin与getline一起使用的时候,要在cin之后加上cin.get()把回车读掉,否则该回车将会被getline读取,从而导致意想不到的错误结果。

  1 #include<iostream>
  2 #include<string>
  3 #include<queue>
  4 #include<set>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<stdio.h>
  8 
  9 using namespace std;
 10 
 11 
 12 int main()
 13 {
 14     int n;
 15     cin >> n;
 16     string s;
 17     vector<string>v;
 18     cin.get();
 19 
 20     for (int i = 0; i < n*2; ++i)
 21     {
 22         getline(cin, s);
 23         v.push_back(s);        //先将输入保存在v中
 24     }
 25 
 26 
 27     for (int i = 0; i < n*2; i += 2)
 28     {
 29         int r1 = i;        //第i行
 30         int r2 = i + 1;    //第i+1行
 31         string str1 = v[r1];
 32         string str2 = v[r2];
 33 
 34         //分别从字符串中获取出时,分,秒
 35         int h1 = (str1[0] - '0') * 10 + (str1[1] - '0');
 36         int m1 = (str1[3] - '0') * 10 + (str1[4] - '0');
 37         int s1 = (str1[6] - '0') * 10 + (str1[7] - '0');
 38 
 39         int h2 = (str1[9] - '0') * 10 + (str1[10] - '0');
 40         int m2 = (str1[12] - '0') * 10 + (str1[13] - '0');
 41         int s2 = (str1[15] - '0') * 10 + (str1[16] - '0');
 42 
 43         int h3 = (str2[0] - '0') * 10 + (str2[1] - '0');
 44         int m3 = (str2[3] - '0') * 10 + (str2[4] - '0');
 45         int s3 = (str2[6] - '0') * 10 + (str2[7] - '0');
 46 
 47         int h4 = (str2[9] - '0') * 10 + (str2[10] - '0');
 48         int m4 = (str2[12] - '0') * 10 + (str2[13] - '0');
 49         int s4 = (str2[15] - '0') * 10 + (str2[16] - '0');
 50 
 51         //算出第一行的时间差
 52         int ss1 = s2 - s1;
 53         int mm1 = m2 - m1;
 54         int hh1 = h2 - h1;
 55 
 56         if (ss1 < 0)
 57         {
 58             ss1 = ss1 + 60;
 59             mm1 = mm1 - 1;
 60         }
 61         if (mm1 < 0)
 62         {
 63             mm1 = mm1 + 60;
 64             hh1 = hh1 - 1;
 65         }
 66         if (hh1 < 0)   //之差小于0就说明是次日或者第三日
 67         {
 68             if (s[20] == '1')    //次日
 69                 hh1 = hh1 + 24;
 70             if (s[20] == '2')    //第三日
 71                 hh1 = hh1 + 48;
 72         }
 73 
 74         //算出第二行的时间差
 75         int ss2 = s4 - s3;
 76         int mm2 = m4 - m3;
 77         int hh2 = h4 - h3;
 78 
 79         if (ss2 < 0)
 80         {
 81             ss2 = ss2 + 60;
 82             mm2 = mm2 - 1;
 83         }
 84         if (mm2 < 0)
 85         {
 86             mm2 = mm2 + 60;
 87             hh2 = hh2 - 1;
 88         }
 89         if (hh2 < 0)   //之差小于0就说明是次日或者第三日
 90         {
 91             if(s[20] == '1')
 92                 hh2 = hh2 + 24;
 93             if (s[20] == '2')
 94                 hh2 = hh2 + 48;
 95         }
 96 
 97         int h = (hh1 + hh2) / 2;    //真正的飞行时间的小时数为两者的一半
 98         int m = mm1;
 99         int s = ss1;
100 
101         printf("%02d:%02d:%02d
", h, m, s);
102     }
103 
104     return 0;
105 }
原文地址:https://www.cnblogs.com/FengZeng666/p/10541792.html