1231. 航班时间(字符串处理+sscanf用法)

题目链接:

https://www.acwing.com/problem/content/1233/

 

题解:

(飞行时间+时差 + 飞行时间 - 时差) / 2 = 飞行时间

 

AC代码:

#include 
#include 
#include 
#include 
using namespace std;

int getSecond(int h,int m,int s){
    return h*3600+m*60+s;
}


int getTime(){
    string t;
    getline(cin,t);
    if(t.back() != ')') t += " (+0)";
    int h1,m1,s1,h2,m2,s2,d;
    sscanf(t.c_str(),"%d:%d:%d %d:%d:%d (+%d)",&h1,&m1,&s1,&h2,&m2,&s2,&d);
    
    return getSecond(h2,m2,s2) - getSecond(h1,m1,s1) + d*24*3600;
}


int main(){
    int n;
    scanf("%d",&n);
    getchar();
    while(n--){
        
        int detTime = (getTime()+getTime())/2;
        int h = detTime / 3600;
        int m = detTime % 3600 / 60;
        int s = detTime % 60;
        
        printf("%02d:%02d:%02d\n",h,m,s);
    }
    
    
    return 0;
}

 

你可能感兴趣的:(1231. 航班时间(字符串处理+sscanf用法))