一、y总sscanf
#include<bits/stdc++.h>
using namespace std;
int get_seconds(int h, int m, int s) {
return h * 3600 + m * 60 + s;
}
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;
}
int main() {
int n;
cin >> n;
string line;
getline(cin, line);
while (n--) {
int time = (get_time() + get_time()) / 2;
int hour = time / 3600, minute = time % 3600 / 60, second = time % 60;
printf("%02d:%02d:%02d\n", hour, minute, second);
}
return 0;
}
二、自己写的
#include<bits/stdc++.h>
using namespace std;
int flyh, flym, flys;
void count_time(int h1, int m1, int s1, int h2, int m2, int s2) {
if (s2 >= s1) {
flys = s2 - s1;
}
else {
m2--;
flys = s2 + 60 - s1;
}
if (m2 >= m1) {
flym = m2 - m1;
}
else {
h2--;
flym = m2 + 60 - m1;
}
flyh = h2 - h1;
}
int main()
{
int q;
cin >> q;
while (q--) {
flyh = 0; flym = 0; flys = 0;
int h1, m1, s1, h2, m2, s2, day1=0;
int h3, m3, s3, h4, m4, s4, day2=0;
scanf("%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1, &h2, &m2, &s2, &day1);
scanf("%d:%d:%d %d:%d:%d (+%d)", &h3, &m3, &s3, &h4, &m4, &s4, &day2);
//cout<<h1<<' '<<m1<<' '<<s1<<' '<<h2<<' '<<m2<<' '<<s2<<' '<<day1<<endl;
int nowh = 0, nowm = 0, nows = 0;
if(day1==2){
count_time(h1,m1,s1,24,0,0);
nowh+=flyh,nowm+=flym,nows+=flys;
nowh+=24;
count_time(0,0,0,h2,m2,s2);
nowh+=flyh,nowm+=flym,nows+=flys;
}
else if(day1==1){
count_time(h1,m1,s1,24,0,0);
nowh+=flyh,nowm+=flym,nows+=flys;
count_time(0,0,0,h2,m2,s2);
nowh+=flyh,nowm+=flym,nows+=flys;
}
else {
count_time(h1, m1, s1, h2, m2, s2);
//cout << flyh << ' ' << flym << ' ' << flys << endl;
nowh += flyh, nowm += flym, nows += flys;
}
if(day2==2){
count_time(h3,m3,s3,24,0,0);
nowh+=flyh,nowm+=flym,nows+=flys;
nowh+=24;
count_time(0,0,0,h4,m4,s4);
nowh+=flyh,nowm+=flym,nows+=flys;
}
else if(day2==1){
count_time(h3,m3,s3,24,0,0);
nowh+=flyh,nowm+=flym,nows+=flys;
count_time(0,0,0,h4,m4,s4);
nowh+=flyh,nowm+=flym,nows+=flys;
}
else{
count_time(h3, m3, s3, h4, m4, s4);
nowh += flyh, nowm += flym, nows += flys;
}
int sum = nowh * 3600 + nowm * 60 + nows;
sum /= 2;
nowh = sum / 3600;
nowm = (sum - nowh * 3600) /60;
nows = (sum - nowh * 3600 - nowm * 60);
printf("%02d:%02d:%02d\n", nowh, nowm, nows);
}
return 0;
}