hdu_1006 Tick and Tick(暴力模拟)
hdu1006
标签(空格分隔): 暴力枚举
这个题要暴力模拟
其实是很简单的,要踏踏实实地反复去读条件,想清楚指针转动的情况就很简单了
/* 秒针的速度s=6°/s,分针是m=1/10°/s,时针是h=1/120°/s;
相对速度s_m=59/10°/s,s_h=719/120°/s,m_h=120/11°/s;
所以相差一度所需要的时间sm=10/59 s/°,sh=120/719 s/°,mh=120/11 s/°;
差360°的周期为cycle_sm=3600/59 s,cycle_sh=43200/719 s,cycle_mh=43200/11 s;
假设开始时从12点整开始,旋转至再均回到12点(即时针转一圈)
*/
double max(double a,double b,double c){
return a>b?(a>c?a:c):(b>c?b:c);}
double min(double a,double b,double c){
return a<b?(a<c?a:c):(b<c?b:c);}
#include <stdio.h>
int main()
{
double d;
double cycle_sm=3600./59.;
double cycle_sh=43200./719.;
double cycle_mh=43200./11.;//这三行是时针、分针、秒针两两相遇的周期
double sum;
double happys,happye;//开始happy和结束happy的时刻
double sm=10./59.,sh=120./719.,mh=120./11.;//相差一度所需要的时间
double d_sm,d_sh,d_mh,not_d_sm,not_d_sh,not_d_mh;//表示相差d°及以上的时刻和不再相差d°及以上的时刻
while(scanf("%lf",&d)!=EOF&&d!=-1)
{
sum=0;
d_sm=sm*d; not_d_sm=cycle_sm-d_sm;
d_sh=sh*d; not_d_sh=cycle_sh-d_sh;
d_mh=mh*d; not_d_mh=cycle_mh-d_mh;
happys=max(d_sm,d_sh,d_mh);
happye=min(not_d_sm,not_d_sh,not_d_mh);
while(happys<=43200&&happye<=43200)//43200是时针针转一圈的秒数
{
happys=max(d_sm,d_sh,d_mh);//两两之间最后一个满足相差d°及以上的条件视为开始happy时刻
happye=min(not_d_sm,not_d_sh,not_d_mh);//两两之间第一个不再满足相差d°及以上视为结束happy的时刻
if(happys<happye)
sum+=happye-happys;//如果end的时间比start的晚,由sum记录并累积
if(happye==not_d_sm)
{d_sm+=cycle_sm;not_d_sm+=cycle_sm;}
else if(happye==not_d_sh)
{d_sh+=cycle_sh;not_d_sh+=cycle_sh;}
else if(happye==not_d_mh)
{d_mh+=cycle_mh;not_d_mh+=cycle_mh;}//happy时间end后最慢的指针要提前一个周期才能让比它快的再次追上
}
printf("%.3lf\n",sum/43200*100);
}
return 0;
}
‘