Dado el ángulo theta, encuentre un tiempo posible (en formato hh:mm) cuando el ángulo entre la manecilla de la hora y la manecilla del reloj sea theta. Si no existe tal tiempo, imprima -1.
Ejemplos:
Input : theta = 90.0 Output : 3:0
Input : theta = 60.0 Output : 2:0
Hemos discutido cómo encontrar el ángulo para un tiempo dado en la publicación a continuación.
Calcular el ángulo entre la manecilla de hora y la manecilla de minutos
En este problema, se nos pide que hagamos lo contrario. Dado que hay 12 posibilidades para la hora y 60 posibilidades para el minuto, recorremos todo el tiempo posible, que es 12*60 = 720, si el ángulo para cualquier tiempo es igual a theta dado, imprimimos ese tiempo.
C++
// C++ program to find time for a given angle. #include <bits/stdc++.h> using namespace std; // function to find angle between // hour hand and minute hand float calcAngle(int hh, int mm) { // Calculate the angles moved by hour and // minute hands with reference to 12:00 float hour_angle = 0.5 * (hh*60 + mm); float minute_angle = 6*mm; // 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; } // function to print all time when angle between // hour hand and minute hand is theta void printTime(float theta) { for (int hh=0; hh<12; hh++) { for (int mm=0; mm<60; mm++) { if (calcAngle(hh, mm)==theta) { printf("%d:%d\n", hh, mm); return; } } } printf("Input angle not valid.\n"); return; } // driver code to test above function int main() { float theta = 90.0; printTime(theta); return 0; }
Java
// Java program to find time // for a given angle. class GFG { // function to find angle between // hour hand and minute hand static float calcAngle(int hh, int mm) { // Calculate the angles moved by hour and // minute hands with reference to 12:00 float hour_angle = 0.5f * (hh * 60 + mm); float minute_angle = 6 * mm; // Find the difference between two angles float 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 print all time when // angle between hour hand and minute // hand is theta static void printTime(float theta) { for (int hh = 0; hh < 12; hh++) { for (int mm = 0; mm < 60; mm++) { if (calcAngle(hh, mm) == theta) { System.out.println(hh + ":" + mm); return; } } } System.out.println("Input angle not valid."); return; } // Driver code public static void main (String[] args) { float theta = 90.0f; printTime(theta); } } // This code is contributed by Anant Agarwal.
Python3
# Python 3 program to find time for a # given angle. # function to find angle between # hour hand and minute hand def calcAngle(hh, mm): # Calculate the angles moved by # hour and minute hands with # reference to 12:00 hour_angle = 0.5 * (hh * 60 + mm) minute_angle = 6 * mm # 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 print all time when # angle between hour hand and minute # hand is theta def printTime(theta): for hh in range(0, 12): for mm in range(0, 60): if (calcAngle(hh, mm)==theta): print(hh, ":", mm, sep = "") return print("Input angle not valid.") return # driver code to test above function theta = 90.0 printTime(theta) # This code is contributed by Smitha
C#
// C# program to find time for a given // angle. using System; class GFG { // function to find angle between // hour hand and minute hand static float calcAngle(int hh, int mm) { // Calculate the angles moved by hour // and minute hands with reference // to 12:00 float hour_angle = 0.5f * (hh * 60 + mm); float minute_angle = 6 * mm; // Find the difference between two angles float 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 print all time when // angle between hour hand and minute // hand is theta static void printTime(float theta) { for (int hh = 0; hh < 12; hh++) { for (int mm = 0; mm < 60; mm++) { if (calcAngle(hh, mm) == theta) { Console.WriteLine(hh + ":" + mm); return; } } } Console.WriteLine("Input angle not valid."); return; } // Driver code public static void Main () { float theta = 90.0f; printTime(theta); } } // This code is contributed by vt_m.
PHP
<?php // PHP program to find // time for a given angle. // Function to find angle between // hour hand and minute hand function calcAngle($hh, $mm) { // Calculate the angles // moved by hour and minute // hands with reference to 12:00 $hour_angle = 0.5 * ($hh * 60 + $mm); $minute_angle = 6 * $mm; // 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 print all // time when angle between // hour hand and minute // hand is theta function printTime( $theta) { for ($hh = 0; $hh < 12; $hh++) { for ($mm = 0; $mm < 60; $mm++) { if (calcAngle($hh, $mm) == $theta) { echo $hh, ":", $mm; return; } } } echo "Input angle not valid.\n"; return; } // Driver Code $theta = 90.0; printTime($theta); // This code is contributed by anuj_67. ?>
Javascript
<script> // JavaScript program to find // time for a given angle. // function to find angle between // hour hand and minute hand function calcAngle(hh, mm) { // Calculate the angles moved by hour and // minute hands with reference to 12:00 var hour_angle = 0.5 * (hh * 60 + mm); var minute_angle = 6 * mm; // 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 print all time // when angle between // hour hand and minute hand is theta function printTime(theta) { for (var hh = 0; hh < 12; hh++) { for (var mm = 0; mm < 60; mm++) { if (calcAngle(hh, mm) === theta) { document.write(hh + ":" + mm + "<br>"); return; } } } document.write("Input angle not valid.<br>"); return; } // driver code to test above function var theta = 90.0; printTime(theta); </script>
Producción :
3:0
Complejidad de Tiempo : O(1)
Espacio Auxiliar : O(1)
Este artículo es una contribución de Pratik Chhajer . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
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