Calcular el ángulo entre la manecilla de hora y la manecilla de minutos

Este problema se conoce como problema del ángulo del reloj, donde necesitamos encontrar el ángulo entre las manecillas de un reloj analógico en un momento dado.
Ejemplos: 

Input:  
h = 12:00
m = 30.00
Output: 
165 degree

Input:  
h = 3.00
m = 30.00
Output: 
75 degree

La idea es tomar como referencia las 12:00 (h = 12, m = 0). Los siguientes son pasos detallados.

1. Calcular el ángulo que forma la manecilla horaria con respecto a las 12:00 en h horas y m minutos. 
2. Calcular el ángulo que forma el minutero con respecto a las 12:00 en h horas y m minutos. 
3. La diferencia entre los dos ángulos es el ángulo entre las dos manos.

¿Cómo calcular los dos ángulos con respecto a las 12:00? 
El minutero se mueve 360 ​​grados en 60 minutos (o 6 grados en un minuto) y el horario se mueve 360 ​​grados en 12 horas (o 0,5 grados en 1 minuto). En h horas y m minutos, el minutero se movería (h*60 + m)*6 y el horario se movería (h*60 + m)*0,5. 
 

C++

// C++ program to find angle between hour and minute hands
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to find minimum of two integers
int min(int x, int y)
{
    return (x < y)? x: y;
     
}
 
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;
      h += 1;
       if(h>12)
        h = h-12;
     }
 
    // Calculate the angles moved
    // by hour and minute hands
    // with reference to 12:00
    float hour_angle = 0.5 * (h * 60 + m);
    float minute_angle = 6 * m;
 
    // Find the difference between two angles
    float angle = abs(hour_angle - minute_angle);
 
    // Return the smaller angle of two possible angles
    angle = min(360 - angle, angle);
 
    return angle;
}
 
// Driver Code
int main()
{
    cout << calcAngle(9, 60) << endl;
    cout << calcAngle(3, 30) << endl;
    return 0;
}
 
// This is code is contributed by rathbhupendra

C

// C program to find angle between hour and minute hands
#include <stdio.h>
#include <stdlib.h>
 
// Utility function to find minimum of two integers
int min(int x, int y) { return (x < y)? x: y; }
 
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;
      h += 1;
       if(h>12)
        h = h-12;
     }
 
 
    // 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;
}
 
// Driver Code
int main()
{
    printf("%d n", calcAngle(9, 60));
    printf("%d n", calcAngle(3, 30));
    return 0;
}

Java

// Java program to find angle between hour and minute hands
import java.io.*;
 
class GFG
{
    // Function to calculate the angle
    static int calcAngle(double h, double m)
    {
        // validate the input
        if (h <0 || m < 0 || h >12 || m > 60)
            System.out.println("Wrong input");
 
        if (h == 12)
            h = 0;
             if (m == 60)
       {
        m = 0;
        h += 1;
        if(h>12)
          h = h-12;
        }
 
 
        // 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);
 
        // smaller angle of two possible angles
        angle = Math.min(360-angle, angle);
 
        return angle;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        System.out.println(calcAngle(9, 60)+" degree");
        System.out.println(calcAngle(3, 30)+" degree");
    }
}
 
// Contributed by Pramod Kumar

Python3

# Python program to find angle
# between hour and minute hands
 
# Function to Calculate angle b/w
# 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
            h += 1;
            if(h>12):
                   h = h-12;
         
        # 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
 
# Driver Code
h = 9
m = 60
print('Angle ', calcAngle(h,m))
 
# This code is contributed by Danish Raza

C#

// C# program to find angle between
// hour and minute hands
using System;
 
class GFG {
     
    // Function to calculate the angle
    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;
        h += 1;
        if(h>12)
          h = h-12;
       }
 
        // 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);
 
        // smaller angle of two possible angles
        angle = Math.Min(360 - angle, angle);
 
        return angle;
    }
     
    // Driver code
    public static void Main ()
    {
        Console.WriteLine(calcAngle(9, 60));
        Console.Write(calcAngle(3, 30));
    }
}
 
// This code is contributed by Nitin Mittal.

PHP

<?php
// PHP program to find
// angle between hour
// and minute hands
 
// Utility function to
// find minimum of two
// integers
function mintwo($x, $y)
{
    return ($x < $y) ?
                  $x : $y;
}
 
function calcAngle($h, $m)
{
    // validate the input
    if ($h <0 || $m < 0 ||
        $h >12 || $m > 60)
        echo "Wrong input";
 
    if ($h == 12) $h = 0;
    if ($m == 60) {
        $m = 0;
        $h += 1;
        if($h>12)
          $h = $h-12;
    }
 
    // 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;
}
 
// Driver Code
echo calcAngle(9, 60), "\n";
echo calcAngle(3, 30), "\n";
 
// This code is contributed by ajit
?>

Javascript

<script>
// Javascript program to find angle between hour and minute hands
 
// Utility function to find minimum of two integers
function min(x, y)
{
    return (x < y)? x: y;
     
}
 
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;
    h += 1;
    if(h>12)
        h = h-12;
    }
 
    // Calculate the angles moved
    // by hour and minute hands
    // with reference to 12:00
    let hour_angle = 0.5 * (h * 60 + m);
    let minute_angle = 6 * m;
 
    // Find the difference between two angles
    let angle = Math.abs(hour_angle - minute_angle);
 
    // Return the smaller angle of two possible angles
    angle = min(360 - angle, angle);
 
    return angle;
}
 
// Driver Code
    document.write(calcAngle(9, 60) + "<br>");
    document.write(calcAngle(3, 30) + "<br>");
     
// This code is contributed by Surbhi Tyagi.
 
</script>
Producción

60
75

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)
Ejercicio: Encuentra todos los momentos en los que las manecillas de horas y minutos se superponen.
Este artículo es una contribución de Ashish Bansal . Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *