AcWing
1231. 航班时间
读数据:scanf("%d:%d:%d (%d)", &a, &b, &c, &d);
,当读取到不满足输入格式的字符会停止读取并回退;
或者这样读:
int get_time(){
string line;
getline(cin, line);
if (line.back() != ')') line += " (+0)";
int h1, m1, s1, h2, m2, s2, d;
sscanf(line.c_str(), "%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1, &h2, &m2, &s2, &d);
return get_seconds(h2, m2, s2) - get_seconds(h1, m1, s1) + d * 24 * 3600;
}
输出结果:printf("%02d:%02d:%02d\n", a, b, c);
,表示长度为2
,长度不足2
的部分用0
填补(只有0
能这样用,默认是空格填补).
#include <bits/stdc++.h>
using namespace std;
int t;
int h11, m11, s11, h12, m12, s12, d1;
int h21, m21, s21, h22, m22, s22, d2;
void cald(){
int sec11 = h11 * 3600 + m11 * 60 + s11;
int sec12 = d1 * 86400 + h12 * 3600 + m12 * 60 + s12;
int sec21 = h21 * 3600 + m21 * 60 + s21;
int sec22 = d2 * 86400 + h22 * 3600 + m22 * 60 + s22;
int dt = (sec12 - sec21 + sec22 - sec11) / 2;
int hh = dt / 3600, mm = dt % 3600 / 60, ss = dt % 60;
printf("%02d:%02d:%02d\n", hh, mm, ss);
}
int main(){
scanf("%d\n", &t);
while(t --){
d1 = 0, d2 = 0;
scanf("%d:%d:%d %d:%d:%d (+%d)", &h11, &m11, &s11, &h12, &m12, &s12, &d1);
scanf("%d:%d:%d %d:%d:%d (+%d)", &h21, &m21, &s21, &h22, &m22, &s22, &d2);
cald();
}
return 0;
}