Detailed Explanation of the "Little Fish Swimming" Problem
Problem Analysis
On this day, Little Fish made precise timing of its swimming time and found that it swam from a time of b minutes to a time of c time of d minutes on the same day. Please help Little Fish calculate how long it swam in total that day.
The key to the problem lies in unifying units.
Unify units to minutes: a hours and b minutes is a * 60 + b minutes. c hours and d minutes is c * 60 + d minutes. The time difference is the difference between the two minutes. Then convert it to hours and minutes.
Conventional Approach
Step 1: Input Time
Use the input command to input the input time, separately input hours and minutes.
1 | a = input('Please enter the starting hour (24-hour format):') |
Step 2: Time Conversion
Convert hours to minutes by multiplying by 60 and adding the input minutes.
1 | e = int(a) * 60 + int(b) |
Step 3: Time Calculation
Subtract the starting time (minutes) from the ending time (minutes) to calculate the total duration.
1 | g = f - e |
Step 4: Convert to Hour Format
Divide the total minutes by 60 to get the integer part, and the remainder is the remaining minutes.
1 | h = g // 60 |
Step 5: Output
Remember to convert the string.
1 | print('Total swimming time: ' + str(h) + ' hours ' + str(i) + ' minutes') |
Code Example
1 | a = input('Please enter the starting hour (24-hour format):') #Waiting for input |
Partial Code Explanation
【//】 is integer division, 【%】 is the remainder
Integer division is to perform division, and only the integer part is retained. The remainder is what you learned in elementary school ↓
Borrowing Method (Not Recommended)
For example: The starting time is 6:30, and the ending time is 7:20. Normally, we can calculate the time in our heads as 50 minutes. In the computer, you need to split the hours and minutes to calculate, i.e., “ending hours - starting hours” “ending minutes - starting minutes”, here also involves a judgment condition, just like the example I gave, the ending minutes are smaller than the starting minutes, so the subtraction will result in a negative number, at this time we need to borrow 1 from the hours and then perform the operation.
Step 1: Input Time
Use the input command to input the input time, separately input hours and minutes.
1 | a = input('Please enter the starting hour (24-hour format):') |
Step 2: Minute Size Judgment
If the ending minutes are greater than the starting minutes, there is no need to borrow, otherwise you need to borrow. First use if to judge, if d < b i.e., the ending minutes are less than the starting minutes, then……
The code is in the next step.
Step 3: Borrowing Operation
In the hours, subtract the ending time from the starting time and subtract 1. In the minutes, because you have borrowed 1, add 60 (one hour is 60 minutes) to the ending minutes, and then subtract the ending minutes to avoid negative numbers.
Remember to output.
1 | if d<b: |
Step 4: Non-Borrowing Operation
If d is greater than b, i.e., the ending minutes are greater than the starting minutes, then direct subtraction can be used here. You need to use else, i.e., otherwise……
You also need to output:
1 | else: |
Code Example
1 | a = input('Please enter the starting hour (24-hour format):') #Waiting for input |
Partial Code Explanation
if statement has not been learned yet, it will be learned later, and the general usage is something like this, detailed please see comments.