给定四个整数H , M , L1和L2 ,它们分别表示12小时制时钟中的小时和分钟,L1和L2分别表示时针和分针的长度。任务是找到时针和分针的端点之间的距离。
例子:
Input: H = 3, M = 30, L1 = 3, L2 = 4
Output: 4.33499
Explanation:
At 3:30, distance between end point of hour hand and minute hand is 4.33499
Input: H = 10, M = 30, L1 = 3, L2 = 4
Output: 6.47898
Explanation:
At 10:30, distance between end point of hour hand and minute hand is 6.47898
方法:想法是找到给定时间的时针和分针之间的角度,然后可以借助余弦公式来计算时针和分针的端点之间的距离–
Distance Between End Points of the Hour and minute hand =
下面是上述方法的实现:
C++
// C++ implementation to find the
// distance between the end points
// of the hour and minute hand
#include
using namespace std;
// Function to find the angle between
// Hour hand and minute hand
int calcAngle(double h, double m)
{
// Validate the input
if (h < 0 || m < 0
|| h > 12 || m > 60)
printf("Wrong input");
if (h == 12)
h = 0;
if (m == 60)
m = 0;
// Calculate the angles moved
// by hour and minute hands
// with reference to 12:00
int hour_angle = 0.5 * (h * 60 + m);
int minute_angle = 6 * m;
// Find the difference
// between two angles
int angle = abs(hour_angle - minute_angle);
// Return the smaller angle
// of two possible angles
angle = min(360 - angle, angle);
return angle;
}
// Function to calculate
// cos value of angle c
float cal_cos(float n)
{
float accuracy = 0.0001, x1,
denominator, cosx, cosval;
// Converting degrees to radian
n = n * (3.142 / 180.0);
x1 = 1;
// Maps the sum
// along the series
cosx = x1;
// Holds the actual
// value of sin(n)
cosval = cos(n);
int i = 1;
do {
denominator = 2 * i * (2 * i - 1);
x1 = -x1 * n * n / denominator;
cosx = cosx + x1;
i = i + 1;
} while (accuracy <= fabs(cosval - cosx));
return cosx;
}
// Function to distance between the
// endpoints of the hour and minute hand
float distanceEndpoints(
int a, int b, float c)
{
float angle = cal_cos(c);
return sqrt((a * a)
+ (b * b)
- 2 * a * b * angle);
}
// Driver Code
int main()
{
// Time
int hour = 3;
int min = 30;
// Length of
// hour hand
int hourHand = 3;
// Length of
// minute hand
int minHand = 4;
// calling Function for
// finding angle
// between hour hand
// and minute hand
double angle = calcAngle(hour, min);
// Function for finding
// distance between
// end points of minute
// hand and hour hand
float distance = distanceEndpoints(
minHand, hourHand, angle);
cout << distance;
return 0;
}
Java
// Java implementation to find the
// distance between the end points
// of the hour and minute hand
class GFG{
// Function to find the angle between
// Hour hand and minute hand
static int calcAngle(double h, double m)
{
// Validate the input
if (h < 0 || m < 0 ||
h > 12 || m > 60)
System.out.printf("Wrong input");
if (h == 12)
h = 0;
if (m == 60)
m = 0;
// Calculate the angles moved
// by hour and minute hands
// with reference to 12:00
int hour_angle = (int)(0.5 * (h * 60 + m));
int minute_angle = (int)(6 * m);
// Find the difference
// between two angles
int angle = Math.abs(hour_angle -
minute_angle);
// Return the smaller angle
// of two possible angles
angle = Math.min(360 - angle, angle);
return angle;
}
// Function to calculate
// cos value of angle c
static float cal_cos(float n)
{
float accuracy = (float) 0.0001, x1,
denominator,
cosx, cosval;
// Converting degrees to radian
n = (float)(n * (3.142 / 180.0));
x1 = 1;
// Maps the sum along
// the series
cosx = x1;
// Holds the actual
// value of sin(n)
cosval = (float)Math.cos(n);
int i = 1;
do
{
denominator = 2 * i * (2 * i - 1);
x1 = -x1 * n * n / denominator;
cosx = cosx + x1;
i = i + 1;
} while (accuracy <= Math.abs(cosval - cosx));
return cosx;
}
// Function to distance between the
// endpoints of the hour and minute hand
static float distanceEndpoints(int a, int b,
float c)
{
float angle = cal_cos(c);
return (float) Math.sqrt((a * a) +
(b * b) -
2 * a * b * angle);
}
// Driver code
public static void main(String[] args)
{
// Time
int hour = 3;
int min = 30;
// Length of
// hour hand
int hourHand = 3;
// Length of
// minute hand
int minHand = 4;
// Calling Function
// for finding angle
// between hour hand
// and minute hand
double angle = calcAngle(hour, min);
// Function for finding
// distance between
// end points of minute
// hand and hour hand
float distance = distanceEndpoints(minHand,
hourHand,
(long)angle);
System.out.printf("%.5f", distance);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to find the
# distance between the end points
# of the hour and minute hand
import math
# Function to find the angle
# between Hour hand and minute
# hand
def calcAngle(h, m):
# Validate the input
if (h < 0 or m < 0 or
h > 12 or m > 60):
print("Wrong input")
if (h == 12):
h = 0
if (m == 60):
m = 0
# Calculate the angles moved
# by hour and minute hands
# with reference to 12:00
hour_angle = 0.5 * (h * 60 + m)
minute_angle = 6 * m
# Find the difference
# between two angles
angle = abs(hour_angle -
minute_angle)
# Return the smaller angle
# of two possible angles
angle = min(360 -
angle, angle)
return angle
# Function to calculate
# cos value of angle c
def cal_cos(n):
accuracy = 0.0001
# Converting degrees to
# radian
n = n * (3.142 / 180.0)
x1 = 1
# Maps the sum
# along the series
cosx = x1
# Holds the actual
# value of sin(n)
cosval = math.cos(n)
i = 1
while True:
denominator = 2 * i * (2 * i - 1)
x1 = -x1 * n * n / denominator
cosx = cosx + x1
i = i + 1
if accuracy > math.fabs(cosval -
cosx):
break
return cosx
# Function to distance between
# the endpoints of the hour
# and minute hand
def distanceEndpoints(a, b, c):
angle = cal_cos(c)
return math.sqrt((a * a) +
(b * b) -
2 * a * b * angle)
# Driver code
# Time
hour = 3
Min = 30
# Length of
# hour hand
hourHand = 3
# Length of
# minute hand
minHand = 4
# calling Function for
# finding angle
# between hour hand
# and minute hand
angle = calcAngle(hour,
Min)
# Function for finding
# distance between
# end points of minute
# hand and hour hand
distance = distanceEndpoints(minHand,
hourHand,
angle)
print ('%.5f' % distance)
# This code is contributed by divyeshrabadiya07
C#
// C# implementation to find the
// distance between the end points
// of the hour and minute hand
using System;
class GFG{
// Function to find the angle between
// Hour hand and minute hand
static int calcAngle(double h, double m)
{
// Validate the input
if (h < 0 || m < 0 ||
h > 12 || m > 60)
Console.Write("Wrong input");
if (h == 12)
h = 0;
if (m == 60)
m = 0;
// Calculate the angles moved
// by hour and minute hands
// with reference to 12:00
int hour_angle = (int)(0.5 * (h * 60 + m));
int minute_angle = (int)(6 * m);
// Find the difference
// between two angles
int angle = Math.Abs(hour_angle -
minute_angle);
// Return the smaller angle
// of two possible angles
angle = Math.Min(360 - angle, angle);
return angle;
}
// Function to calculate
// cos value of angle c
static float cal_cos(float n)
{
float accuracy = (float) 0.0001, x1,
denominator,
cosx, cosval;
// Converting degrees to radian
n = (float)(n * (3.142 / 180.0));
x1 = 1;
// Maps the sum along
// the series
cosx = x1;
// Holds the actual
// value of sin(n)
cosval = (float)Math.Cos(n);
int i = 1;
do
{
denominator = 2 * i * (2 * i - 1);
x1 = -x1 * n * n / denominator;
cosx = cosx + x1;
i = i + 1;
} while (accuracy <= Math.Abs(cosval - cosx));
return cosx;
}
// Function to distance between the
// endpoints of the hour and minute hand
static float distanceEndpoints(int a, int b,
float c)
{
float angle = cal_cos(c);
return (float) Math.Sqrt((a * a) +
(b * b) -
2 * a * b * angle);
}
// Driver code
public static void Main()
{
// Time
int hour = 3;
int min = 30;
// Length of
// hour hand
int hourHand = 3;
// Length of
// minute hand
int minHand = 4;
// Calling Function
// for finding angle
// between hour hand
// and minute hand
double angle = calcAngle(hour, min);
// Function for finding
// distance between
// end points of minute
// hand and hour hand
float distance = distanceEndpoints(minHand,
hourHand,
(long)angle);
Console.Write(distance);
}
}
// This code is contributed by Code_Mech
输出:
4.33499