HDU 1006 模擬
Tick and Tick
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20120 Accepted Submission(s): 5262
Output For each D, print in a single line the percentage of time in a day that all of the hands are happy, accurate up to 3 decimal places.
Sample Output 100.000 0.000 6.251
Author PAN, Minghao
Source ZJCPC2004 分析:
三個指針走的角速度:
秒針速度S = 6°/s,分針速度M = (1/10)°/s,時針速度H = (1/120)°/s
這三個指針兩兩之間的相對速度差為:
秒時相差S_H = (719/120)°/s,秒分相差S_M = (59/10)°/s,分時相差M_H = (120/11)°/s
相差一度需要的時間為
秒時相差SH = (120/719)s/度,秒分相差SM = (10/59)s/度,分時相差MH = (120/11)s/度
相差360°需要的時間為
秒時相差tSH = 43200.0/719,秒分相差tSM = 3600.0/59,分時相差tMH = 43200.0/11
算出兩兩指針在43200s(12小時)內滿足條件時間的區間的交集。
代碼:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
///秒針角速度 6度/s, 分針0.1度/s,時針1/120度/s
const double sh = 719.0/120 , sm = 59.0/10, mh = 11.0/120;///三個指針兩兩相對角速度。
const double tsh = 43200.0 / 719, tsm = 3600.0 / 59, tmh = 43200.0 / 11;///三個指針兩兩相差360度所需的時間。
///將相對角速度變成周期。(即兩針間需要多久出現夾角的循環)
/// 同樣可求得三個周期的最小公倍數為 43200 秒,即12小時,
double max1(double a, double b, double c)
{
return max(a, max(b, c));
}
double min1(double a, double b, double c)
{
return min(a, min(b, c));
}
int main(void)
{
int D;
while(scanf("%d", &D), D != -1)
{
double bsh, bsm, bmh, esh, esm, emh, total, beginn, endd;
///第一次滿足條件的時間。(開始時間)
bsh = D / sh;
bsm = D / sm;
bmh = D / mh;
///第一次出現不滿足條件的時間。(結束時間)
esh = (360 - D) / sh;
esm = (360 - D) / sm;
emh = (360 - D) / mh;
total = 0;
for(double bt1 = bsh, et1 = esh; et1<= 43200.000001; bt1 += tsh, et1 += tsh)
{
for(double bt2 = bsm, et2 = esm; et2 <= 43200.000001; bt2 += tsm, et2 += tsm)
{
///判斷是否有交集。
if(bt2 > et1)
break;
if(et2 < bt1)
continue;
for(double bt3 = bmh, et3 = emh; et3 <= 43200.000001; bt3 += tmh, et3 += tmh)
{
if(bt3 > et1 || bt3 > et2)
break;
if(et3 < bt1 || et3 < bt2)
continue;
beginn = max1(bt1, bt2, bt3);
endd = min1(et1, et2, et3);
total += (endd - beginn);
}
}
}
printf("%.3f\n", total / 432);
}
return 0;
}
HDU 1006 模擬