https://www.acwing.com/problem/content/1233/
视频:https://www.acwing.com/video/662/
假设两地时差为t,出发时间为当地时间的t1 = h1:m1:s1,到达时间为当地的t2 = h2:m2:s2
返程的出发时间为当地时间的t3 = h3:m3:s3,到达时间为当地的t4 = h4:m4:s4
假设飞行时长为T,有:
t2 - t1 = t + T
t3 - t4 = -t + T
则可以得出:
(t2 - t1) + (t3 - t4) = 2T
本题的难点就在于解题的思路和输入格式的限制,输入格式主要是后面的天数不一定存在
//默认天数为0
#include<bits/stdc++.h>
using namespace std;
int get_time()
{
int h1, h2, m1, m2, s1, s2, d = 0;
scanf("%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1, &h2, &m2, &s2, &d);
return d * 24 * 3600 + (h2 - h1) * 3600 + (m2 - m1) * 60 + m2 - m1;
}
int main()
{
int n;
cin >> n;
while(n --)
{
int t = (get_time() + get_time()) / 2;
printf("%02d:%02d:%02d/n", t / 3600, t % 3600 / 60, t % 60);
}
return 0;
}
//通过getline得到字符串,对字符串进行修改,再对字符串进行读取
#include<bits/stdc++.h>
using namespace std;
int get_time()
{
string line;
getline(cin, line);
int h1, h2, m1, m2, s1, s2, d;
if(line.back() != ‘)‘) line += " (+0)";
sscanf(line.c_str(), "%d:%d:%d %d:%d:%d (+%d)", h1, m1, s1, h2, m2, s2, d);
return d * 24 * 3600 + (h2 - h1) * 3600 + (m2 - m1) * 60 + m2 - m1;
}
int main()
{
int n;
cin >> n;
while(n --)
{
int t = (get_time() + get_time()) / 2;
printf("%02d:%02d:%02d/n", t / 3600, t % 3600 / 60, t % 60);
}
return 0;
}
c_str()函数是string的库函数,返回值是一个const char *类型的指针,指向字符串的首地址
与scanf相似,sscanf读取的是字符串, scanf读取的是键盘输入的字符
原文:https://www.cnblogs.com/fengzhaoning/p/14477898.html