问题
I'm trying to workout this problem, but I am still struggling to understand the logic to solve this problem.
hour degree = 360 / 12 = 30
minutes degree = 360 / 12 / 60 = 0.5
So, according to this, I thought I could formulate the following function in python:
def clockangles(hour, min):
return (hour * 30) + (min * 0.5)
For the hour, it works fine, as it appears to have a 1=1 mapping. But for the minute there is one problem at least. When it's 0 minutes, the minutes hand points to 12.
For example:
7pm: hands pointing to 7pm and minutes pointing to 12
How do I calculate the minutes properly? Please help me understand the formula.
EDIT: For example, if I call the function above with 7pm, e.g clockangles(7,0) I get the value 210. However, according this link the angle at 7:00 is 150
回答1:
Okay. You are trying to find the angle between the two hands. Then this:
minutes degree = 360 / 12 / 60 = 0.5
Is just the number of degrees the hour hand moves per minute. Think about it - the minute hand travels a full 360 each hour. Therefore there are only 60 minutes in a full revolution. 360/60 = 6 degrees per minute for the minute hand.
So, you need to find the difference between the hour and the minute hand. Thus the function now looks like:
def clockangles(hour, minute):
return (hour * 30 + minute * 0.5) - (minute * 6)
Now, this is valid, so we could stop here. However I should explain that this can give both answers larger than 180 degrees and negative angles. If you don't want those things (and from your comments it appears that you don't), correct for them.
def clockangles(hour, minute):
return abs((hour * 30 + minute * 0.5) - (minute * 6))
Now, no negative angles.
def clockangles(hour, minute):
ans = abs((hour * 30 + minute * 0.5) - (minute * 6))
return min(360-ans,ans)
Now, the shorter of the two angles formed by measuring clockwise and counterclockwise.
回答2:
This is not all that difficult if you think about it. Lets consider each hand in isolation first. The minutes hand of the clock rotates 360 degrees in 60 minutes so each minute represents 6 degrees. The hours hand of the clock rotates 360 degrees in 12 hours so we know it moves a total of 30 degrees after each hour, but you need to factor in the advancement of the hours hand between hours. i.e. at 3:30 the minutes hand is on 6 and the hours hand has progressed past 3. We can calculate this advancement simply by (minutes/60) * 30 degrees which is equivalent to minutes/2. So once we know the degrees of each hand we simply find the difference. And formula will be like
degrees = Math.Abs(((hour*30.0 + minute/2.0) - minute*6.0) % 360)
回答3:
Use the algorithm:
1.Minute angle = 360 * minutes / 60
2.Hour angle = [ 360 * (hour % 12) / 12 ] + [ 360 * (minutes / 60) * (1 / 12) ]
3.Angle between hour and minute = (hour angle - minute angle) % 360
this reduces to 30 * hours - 5.5 * minutes.
回答4:
Multiply hours by 60 that is convert it into minutes. hours*60=minutes
Now add the given minutes and converted minutes.
given minutes + converted mintes = total minutes
Now divide the total minutes by 2, that is to find its average. total minutes / 2
Now multiply given minutes by 6. given minutes * 6
Now subtract point 3 from point 4.
By this method you will get the accurate answer.
回答5:
In the following solution, the variable m refers to minutes, and the variable h to hours.
Let's separate the problem into its components.
- Find the angle of the minute hand from 12 o'clock.
- Find the angle of the hour hand from 12 o'clock.
- Find the absolute value of their difference.
Now, let's start solving each component.
- The minute hand makes a full cycle every hour, or 60 minutes. Therefore, we can get the completed percentage of the cycle of the minute hand by
(m / 60)
. Since there are 360 degrees, we can get the angle of the minute hand from 12 o'clock by(m / 60) * 360
. The hour hand makes a full cycle every 12 hours. Since there are 24 hours in a day, we need to normalize the hour value to 12 hours. This is accomplished by
(h % 12)
, which returns the remainder of the hour value divided by 12.Now, as the minute hand makes its cycle, the hour hand does not just remain at the exact value of
(h % 12)
. In fact, it moves 30 degrees between(h % 12)
and(h % 12) + 1
. The amount by which the hour hand deviates from(h % 12)
can be calculated by adding to(h % 12)
the completed percentage of the cycle of the minute hand, which is(m / 60)
. Overall, this gives us(h % 12) + (m / 60)
.Now that we have the exact position of the hour hand, we need to get the completed percentage of the cycle of the hour hand, which we can get by
((h % 12) + (m / 60)) / 12
. Since there are 360 degrees, we can get the angle of the hour hand from 12 o'clock by(((h % 12) + (m / 60)) / 12) * 360
.Now that we have both the angle of the minute and hour hand from 12 o'clock, we simply need to find the difference between the two values, and take the absolute value (since the difference can be negative).
So overall, we have
abs(((((h % 12) + (m / 60)) / 12) - (m / 60)) * 360)
.
Below is a python function that calculates this value. It will return whichever value of the angle that is the shortest.
def find_angle(h, m):
if abs(((((m/60)+(h%12))/12)-(m/60))*360) > 180:
return 360 - abs(((((h % 12) + (m / 60)) / 12) - (m / 60)) * 360)
return abs(((((h % 12) + (m / 60)) / 12) - (m / 60)) * 360)
回答6:
I have worked on this problem and created an equation:
(hr*30)+(min/2)-(min*6)
or
(min*6)-(hr*30)-(min/2)
回答7:
h = int(input())
m = int(input())
angle = float(abs(11 /2 * m - 30 * h))
print(" {}:{} makes the following angle {}°".format(h, m, angle))
回答8:
Note :
60min = 360deg
1min = 6deg
eg:-
consider time as 2:20 (ie. 2hr 20min)
abs((2hr*5) min - (20)min) = 10min
angle_1 = 10min x 6deg = 60deg
angle_2 = 360deg - 60deg = 300deg (means the angle on the other side)
So out of the two angles angle_1 is small.
hence, min_angle = 60deg
def min_angle_bet_hr_min(hr, min):
angle_1 = (hr*5 - min)*6
angle_2 = 360 - angle_1
if angle_1 < angle_2:
min_angle = angle_1
elif angle_1 > angle_2:
min_angle = angle_2
else:
min_angle = 0
return abs(min_angle)
回答9:
def clock_angle(time):
constH = 360 / 12
constM = 360 / 60
if ":" in time:
clock = time.split(":")
c1 = int(clock[0])
c2 = int(clock[1])
if c1 >= 12:
c1 -= 12
rc1 = c1*constH + constH*(c2/60)
rc2 = c2*constM
if rc1 > rc2:
result = (rc1-rc2)
angle2 = 360-rc1+rc2
else:
result = (rc2-rc1)
angle2 = 360-rc2+rc1
if angle2 < result:
result = angle2
return result
return 0
来源:https://stackoverflow.com/questions/20601427/how-do-i-calculate-the-angle-between-the-hour-and-minutes-hands