Acwing game time C++ python

 45 days until the Blue Bridge Cup

topic link

Problem Description:

According to the general idea of ​​y, complex multiple judgment statements are avoided by converting it into minute calculation.

First declare that the maximum time does not exceed 24h

Just consider two cases: for example, the start time is (7:20) and the end time is (8:30)

The start time is equivalent to 7*60+20=(th) 440 minutes, and the end time is equivalent to 8*60+30=(th) 510 minutes

After 510-440=70 minutes, 1 hour 10min

However, the elapsed minutes must be greater than 0, provided that the h of the end time is greater than the h of the start time

If the start time is (7:20), then if the end time is in the range of (7:20) to (24:00), it can be calculated like this.

But if the end time falls from (0:00) to (7:20) then it means that the start time passes 24:00 from (7:20), and then reaches the end time from 0:00, so the real elapsed minutes

is (24*60-the number of minutes corresponding to starttime) + the number of minutes corresponding to endtime

To sum up: Elapsed time = endtime-starttime (corresponding minutes) if endtime-starttime>=0

Elapsed time=1440-starttime+endtime (corresponding minutes) if endtiime-starttime<0

C++ 

#include <cstdio>
#include <iostream>

using namespace std;

int main(){
    int a,b,c,d,starttime,endtime;
    cin>>a>>b>>c>>d;
    starttime=a*60+b;
    endtime=c*60+d;
    if (starttime<endtime) cout<<"O JOGO DUROU "<<(endtime-starttime)/60<<" HORA(S) E "<<(endtime-starttime)%60<<" MINUTO(S)";
    else cout<<"O JOGO DUROU "<<(1440+endtime-starttime)/60<<" HORA(S) E "<<(1440+endtime-starttime)%60<<" MINUTO(S)";
    
    
}

Python: Xiaozheng solves it in three lines (it is not recommended to write this way, it is unnecessary to highlight short and short, it is not easy to understand)

a,b,c,d=map(int,input().strip().split())

start,end=a*60+b,c*60+d

print("O JOGO DUROU %d HORA(S) E %d MINUTO(S)"%((end-start)/60,(end-start)%60)) if start<end else print("O JOGO DUROU %d HORA(S) E %d MINUTO(S)"%((1440+end-start)/60,(end-start)%60))

 Recommended writing method: write clearly step by step is good code

a,b,c,d=map(int,input().strip().split())

start=a*60+b
end=c*60+d

if start<end:
    print("O JOGO DUROU %d HORA(S) E %d MINUTO(S)"%((end-start)/60,(end-start)%60)) 
else:
    print("O JOGO DUROU %d HORA(S) E %d MINUTO(S)"%((1440+end-start)/60,(end-start)%60))

I'm Xiao Zheng, and I'm rushing to love and rush to the mountains and seas!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324220388&siteId=291194637