📜  在给定的时间间隔内时针和分针走过的距离

📅  最后修改于: 2021-04-29 17:27:46             🧑  作者: Mango

给定两个数字HM ,它们分别表示时针和分针的长度以及两个时间间隔(例如T1和T2 ),其形式为HH:MM ,任务是找出时针T1和分针在时间T1之间经过的距离和T2

例子:

方法:

  1. 通过使用本文讨论的方法,找出两个时间间隔T1和T2之间的差异。
  2. 将上面获得的持续时间更改为分钟,如下所示:
    Total Minutes = hours * 60 + minutes
    
  3. 由于分针在60分钟内旋转一圈,因此分针所覆盖的旋转数(例如rm )由下式给出: rm = \frac{total Minutes}{60}
  4. 由于时针在60 * 12 = 720分钟内覆盖一圈,因此,时针所覆盖的转数(例如rh )由下式给出: rh = \frac{total Minutes}{720}
  5. 时针经过的总距离为Total Distance = 2*Pi*H*rh
  6. 分针经过的总距离为Total Distance = 2*Pi*M*rm

下面是上述方法的实现:

Python
# Python program for the above approach
  
import math 
  
# Function to remove ':' and convert
# it into an integer 
def removeColon(s): 
    if (len(s) == 4):
        s = s[:1]+s[2:]
      
    if (len(s) == 5): 
        s = s[:2]+s[3:]
      
    return int(s)
  
# function which find the difference
# between time interval
def diff(s1, s2):
      
    # Change string as 2:21 to 221
    time1 = removeColon(s1)
    time2 = removeColon(s2) 
  
    # Difference between hours 
    hourDiff = time2 // 100 - time1 // 100 - 1; 
  
    # Difference between minutes 
    minDiff = time2 % 100 + (60 - time1 % 100) 
  
    if (minDiff >= 60):
        hourDiff+= 1
        minDiff = minDiff - 60
  
    # Convert answer again in string
    # with ':' 
    res = str(hourDiff) + ':' + str(minDiff)
    return res
  
# Function for finding the distance travelled 
# by minute hand and hour hand
def disTravel(T1, T2, M, H):
      
    # Find the duration between these time
    dur = diff(T1, T2)
      
    # Remove colom from the dur and 
    # convert in int
    s = removeColon(dur)
  
    # Convert the time in to minute 
    # hour * 60 + min 
    totalMinute =(s//100)*60 + s % 100
  
    # Count min hand rotation
    rm = totalMinute / 60;
  
    # Count hour hand rotation
    rh = totalMinute / 720;
  
    # Compute distance traveled by min hand
    minuteDistance = rm * 2* math.pi * M;
  
    # Compute distance traveled by hour hand
    hourDistance = rh * 2* math.pi * H;
      
    return minuteDistance, hourDistance
  
  
# Driver Code
  
# Given Time Intervals
T1 ="1:30"
T2 ="10:50"
  
# Given hour and minute hand length 
H = 5
M = 7
  
# Function Call
minuteDistance, hourDistance = disTravel(T1, T2, M, H)
  
# Print the distance traveled by minute
# and hour hand
print("Distance traveled by minute hand: "
      ,minuteDistance)
print("Distance traveled by hour hand: "
      ,hourDistance )


输出:
Distance traveled by minute hand:  410.50144006906635
Distance traveled by hour hand:  24.434609527920614