class
GFG{
static
int
calcAngle(
double
h,
double
m)
{
if
(h <
0
|| m <
0
||
h >
12
|| m >
60
)
System.out.printf(
"Wrong input"
);
if
(h ==
12
)
h =
0
;
if
(m ==
60
)
m =
0
;
int
hour_angle = (
int
)(
0.5
* (h *
60
+ m));
int
minute_angle = (
int
)(
6
* m);
int
angle = Math.abs(hour_angle -
minute_angle);
angle = Math.min(
360
- angle, angle);
return
angle;
}
static
float
cal_cos(
float
n)
{
float
accuracy = (
float
)
0.0001
, x1,
denominator,
cosx, cosval;
n = (
float
)(n * (
3.142
/
180.0
));
x1 =
1
;
cosx = x1;
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;
}
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);
}
public
static
void
main(String[] args)
{
int
hour =
3
;
int
min =
30
;
int
hourHand =
3
;
int
minHand =
4
;
double
angle = calcAngle(hour, min);
float
distance = distanceEndpoints(minHand,
hourHand,
(
long
)angle);
System.out.printf(
"%.5f"
, distance);
}
}