Blue Bridge Cup preparation – AcWing 668 Game time 2
lanqiao preparation series description
- This is the first blog to prepare for the competition. This time, I use the ACwing question brushing website of President y to prepare for the competition (which can also be regarded as advertising president y hhh)
- The brush questions in this series still hope to promote learning by competition and motivate yourself to learn more knowledge on the basis of preparing for the competition.
- I won't repeat anything else. I hope I can win the prize
subject
Title Description
Read four integers a, B, C and D to represent the start time and end time of the game.
Where A and B are the hours and minutes at the start time, and C and D are the hours and minutes at the end time.
Please calculate the duration of the game.
The minimum duration of the game is 1 minute and the maximum duration is 24 hours.
Input format
A total of one line, including four integers a, B, C and D.
Output format
The output format is o Jogo durou X Hora (s) e y minute (s), indicating that the game lasted X hours and Y minutes.
Data range
0≤A,C≤23,
0≤B,D≤59
Input example 1:
7 8 9 10
Output example 1:
O JOGO DUROU 2 HORA(S) E 2 MINUTO(S)
Input example 2:
7 7 7 7
Output example 2:
O JOGO DUROU 24 HORA(S) E 0 MINUTO(S)
Input example 3:
7 10 8 9
Output example 3:
O JOGO DUROU 0 HORA(S) E 59 MINUTO(S)
The following is a detailed explanation of the solution ideas (three ideas and solutions are provided here)
1, Discuss directly according to the time attribute (the most complex cases are the most considered)
1. Ideas
For this topic, if it is discussed according to the time attribute, it can be divided into two sub attributes - hour and minute.
When analyzing these two attributes, we need to consider the case that the start time is less than the end time (in this case, we need to borrow one from the hour);
Then, a simple analysis of the two situations shows that it is more convenient to classify and discuss minutes (the situation is easy to be combined + the hour needs to be reduced by one minute, which is also controlled by the situation);
2. Graphic analysis
- Discussion on hour
- Discussion on min
After analyzing the above two situations, it is easy to find that the analysis of minutes is the most scientific and effective (that is, it is classified according to whether to "borrow".
3. Implementation code (C + +)
# include <iostream> using namespace std; int main(void){ int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); int hour,min; if(b<d){ min = d-b; if (a<=c) hour = c-a; else hour = (24-a)+c; }else if (b>d) { min = (60-b)+d; if (a<c) hour = c-a-1; //Note that all situations here need to be reduced by one (for an hour) because the loss of a minute needs to be borrowed by one else hour = (23-a)+c; }else { min = 0; if (a<c) hour = c-a; else hour = (24-a)+c; } printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)",hour,min); return 0; }
2, Directly convert the problem of time into the problem of quantity indirectly
1. Ideas
The main idea is to bypass the conversion between the two different units of hour and minute, and convert the two times into the minute system, so as to avoid the headache of borrowing.
2. Specific solutions
There are two specific solutions. I prefer the second one (i.e. y general treatment method)
- Compare and classify the minute values after conversion at two time points;
- Deal with the positive and negative of the difference value after conversion at two time points (the direct classification with spend_time is more elegant, which also reflects the characteristic of single export).
3. Implementation code (C + +)
#include <iostream> using namespace std; int main(void){ int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); int t1,t2,t; //t1 is the first time converted into minutes //t2 is the second time, converted to minutes //t is the minute of the time difference t1 = a*60+b; t2 = c*60+d; if(t1<t2){ t = t2-t1; printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)",t/60,t%60); }else{ t = (1440-t1)+t2; printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)",t/60,t%60); } return 0; }
//y total code 666 directly focuses on spend_time processing #include <iostream> using namespace std; int main(void){ int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); int start,end,spend_time; start = a*60+b; end = c*60+d; spend_time = end-start; if(spend_time<=0) spend_time += 1440; printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)",spend_time/60,spend_time%60); return 0; }