HDU1006 :Tick and Tick 数学+枚举
题意:现在有一个普通的时钟,有时针、分针、秒针,(秒针是连续走,而不是一秒一秒的走)给你一个度数D(小于等于120°)。求:24小时中,三个指针超过这个度数的时间占所有时间的百分比是多少。保留小数点后三位。
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
// 角速度差
static final double s_m = 59.0 / 10, s_h = 719.0 / 120, m_h = 11.0 / 120;
// 相差360°的时间
static final double tsm = 3600.0 / 59, tsh = 43200.0 / 719,
tmh = 43200.0 / 11;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.000");
double D, v = 43200.0, eq = 1e-7;
while (in.hasNext()) {
D = in.nextDouble();
if (D == -1)
break;
double bsm, bsh, bmh, esm, esh, emh, Begin, End, ans = 0;
// 计算各自第一次满足条件的时间
bsm = D / s_m;
bsh = D / s_h;
bmh = D / m_h;
// 计算各自第一次不满足条件的时间
esm = (360 - D) / s_m;
esh = (360 - D) / s_h;
emh = (360 - D) / m_h;
for (double b1 = bsm, e1 = esm; e1 - v < eq; b1 += tsm, e1 += tsm) {
for (double b2 = bsh, e2 = esh; e2 - v < eq; b2 += tsh, e2 += tsh) {
if (e2 < b1)
continue;
if (b2 > e1)
break;
for (double b3 = bmh, e3 = emh; e3 - v < eq; b3 += tmh, e3 += tmh) {
if (e3 < b1 || e3 < b2)
continue;
if (b3 > e1 || b3 > e2)
break;
Begin = Max(b1, b2, b3);// 开始时间最晚
End = Min(e1, e2, e3);// 结束时间最早
ans += (End - Begin);
}
}
}
System.out.println(df.format(ans / 432));
}
}
private static double Max(double a, double b, double c) {
return Math.max(a, Math.max(b, c));
}
private static double Min(double a, double b, double c) {
return Math.min(a, Math.min(b, c));
}
}