Then what time is it (15 minutes)
Sometimes people use four digits to represent a time. For example, 1106 means 11:06. Now, your program calculates the end time based on the start time and the elapsed time.
Read in two numbers. The first number represents the current time with such four digits, and the second number represents the minutes. Calculate the time after so many minutes, and the result is also expressed as four digits. When the hour is a single digit, there is no leading zero, for example, 5:30 represents 530; 0:30 means 030. Note that the second number may represent more than 60 minutes or a negative number.
Input format:
Input gives 2 integers in one line, which are the starting time represented by four digits and the elapsed minutes, separated by spaces. Note: in the start time, when the hour is a single digit, there is no leading zero, that is, 5:30 is 530; 0:30 means 030. The number of minutes passed may be more than 60 or negative.
Output format:
Output the termination time represented by four digits. When the hour is a single digit, there is no leading zero. The title ensures that the start time and end time are within the same day.
Input example:
1120 110
Output example:
1310
Analysis: the difficulty of this problem lies not in the solution, but in the conversion of formatting. On this problem, we use three languages (c, python and java) for implementation, which are two ideas respectively.
Author's request: if the bloggers don't find the answer here, they are welcome to send me a private letter. Generally, I'm online in the afternoon and can send it to the bloggers alone. I'm also a rookie. I hope to make progress and grow up with you.
C language implementation code: (idea: hour and hour calculation, minute and minute calculation)
#include<stdio.h> int main() { int ztime=0,zhtime=0,zmtime=0; int durtime=0,dhtime=0,dmtime=0; scanf("%d %d",&ztime,&durtime); dhtime=durtime/60; dmtime=durtime%60; zhtime=ztime/100; zmtime=ztime%100; zmtime +=dmtime; if(zmtime>=60) { zmtime -=60; zhtime +=1; } if(zmtime<0) { zmtime +=60; zhtime -=1; } zhtime +=dhtime; printf("%d%02d",zhtime,zmtime);//Format the output, because we need to output three digits when our hour is zero return 0; }
java code implementation: (the idea of implementation is the same as that of C language, but it is different when considering formatting output. We must pay attention to that the output format of java is different from that of C language and python. Because we see that many bloggers do not have a java solution to this problem, we write here. I don't know if it can help you!)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); int ztime=input.nextInt(); int durtime=input.nextInt(); int zhtime=ztime/100; int zmtime=ztime%100; int dhtime=durtime/60; int dmtime=durtime%60; zmtime +=dmtime; if(zmtime>=60) { zmtime -=60; zhtime +=1; } if(zmtime<0) { zmtime +=60; zhtime -=1; } zhtime=zhtime+dhtime; if(zhtime==0)//Solve the test point: the minute is less than 0, the output is less than 4 bits, and the hour is 0 { String str =String.format("%03d",zmtime);//Formatting Numbers System.out.print(str); } else { System.out.print(zhtime*100+zmtime); } } }
python code implementation: (idea: python's idea is different from the first two. First, convert hours into minutes, and all calculations are calculated in minutes. Many judgment statements can be omitted in the middle, so this is also the method I recommend. Of course, C language and java can also be written in this way, but the problems we pay attention to will not change.)
time, durtime = map(int, input().split()) zH = time // 100 # get hours for days zM = time % 100 #Gets the minutes of the input time newTime = zH * 60 + zM + durtime #Convert the hours into minutes and add the elapsed time newH = newTime // 60# convert the resulting time into hours and minutes newM = newTime % 60 print('%d%02d' % (newH, newM)) #For the output result, special attention should be paid to that if the minute is less than two digits, 0 should be filled in the front