程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Acwing game time c++ Python

編輯:Python

  It's still... Away from the Blue Bridge Cup 45 God

Topic link

Problem description :

according to y The general idea , Calculate by converting to minutes , Avoid complex multiple judgment statements .

First, state that the maximum time is no more than 24h

There are only two situations to consider : For example, the starting time is (7:20) The end time is (8:30)

The start time is equivalent to 7*60+20=( The first )440 minute , End time equivalent to 8*60+30=( The first )510 minute

After 510-440=70 minute namely 1 hour 10min

however , The number of minutes elapsed should be greater than 0, The premise is that the end time h Greater than the start time h

If the start time is (7:20) , So if the end time is (7:20) To (24:00) Within the scope of , Can be calculated in this way .

But if the end time falls on (0:00) To (7:20) That means the start time is from (7:20) After 24:00, Again from 0:00 Arrival end time , So the number of minutes actually elapsed

by (24*60-starttime The corresponding number of minutes )+endtime The corresponding number of minutes

Sum up : Time passed =endtime-starttime( The corresponding number of minutes ) if endtime-starttime>=0

Time passed =1440-starttime+endtime( The corresponding number of 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 Sanxing solves the problem ( It's not recommended to write , There is no need to be short in order to highlight it , Not easy to understand but )

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))

  Recommend writing : Writing clear 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 I'm going to love mountains and seas !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved