找出时针和分针之间的角度为 theta 的时间
给定角度 theta,找到时针和时钟指针之间的角度为 theta 时的可能时间(以 hh:mm 格式)。如果不存在这样的时间,则打印 -1。
例子 :
Input : theta = 90.0
Output : 3:0
Input : theta = 60.0
Output : 2:0
我们已经讨论了如何在下面的帖子中找到给定时间的角度。
计算时针和分针之间的角度
在这个问题中,我们被要求做反向。由于小时有 12 种可能性,分钟有 60 种可能性,我们循环遍历所有可能的时间,即 12*60 = 720,如果任何时间的角度等于给定的 theta,则我们打印该时间。
C++
// C++ program to find time for a given angle.
#include
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
Javascript
输出 :
3:0
时间复杂度:O(1)
辅助空间:O(1)