Dados cuatro números enteros H , M , L1 y L2 , que denota la hora como una hora y minutos en un reloj de formato de 12 horas y L1 y L2 denotan la longitud de la manecilla de hora y la manecilla de minutos respectivamente. La tarea es encontrar la distancia entre los puntos finales de la manecilla de horas y minutos.
Ejemplos:
Entrada: H = 3, M = 30, L1 = 3, L2 = 4
Salida: 4,33499
Explicación:
A las 3:30, la distancia entre el punto final de la manecilla de hora y la manecilla de minutos es 4,33499
Entrada: H = 10, M = 30, L1 = 3, L2 = 4
Salida: 6,47898
Explicación:
A las 10:30, la distancia entre el punto final de la manecilla de hora y la manecilla de minutos es 6,47898
Enfoque: La idea es encontrar el ángulo entre la manecilla de hora y la manecilla de minutos en el momento dado y luego la distancia entre los puntos finales de la manecilla de hora y la manecilla de minutos se puede calcular con la ayuda de las fórmulas del coseno :
Distancia entre los puntos finales de la manecilla de horas y minutos =
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to find the // distance between the end points // of the hour and minute hand #include <bits/stdc++.h> 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
Javascript
<script> // Javascript implementation to find the // distance between the end points // of the hour and minute hand // Function to find the angle between // Hour hand and minute hand function calcAngle(h, m) { // Validate the input if (h < 0 || m < 0 || h > 12 || m > 60) document.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 var hour_angle = 0.5 * (h * 60 + m); var minute_angle = 6 * m; // Find the difference // between two angles var 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 function cal_cos(n) { var 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 = Math.cos(n); var 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 function distanceEndpoints( a, b, c) { var angle = cal_cos(c); return Math.sqrt((a * a) + (b * b) - 2 * a * b * angle); } // Driver Code // Time var hour = 3; var min = 30; // Length of // hour hand var hourHand = 3; // Length of // minute hand var minHand = 4; // calling Function for // finding angle // between hour hand // and minute hand var angle = calcAngle(hour, min); // Function for finding // distance between // end points of minute // hand and hour hand var distance = distanceEndpoints( minHand, hourHand, angle); document.write( distance.toFixed(5)); </script>
4.33499