您的位置:首页 > 其它

hdu1006-Tick and Tick

2013-08-25 10:14 309 查看

Tick and Tick

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7671    Accepted Submission(s): 2109


[align=left]Problem Description[/align]
The three hands of the clock are rotating every second and meeting each other many times everyday. Finally, they get bored of this and each of them would like to stay away from the other two. A hand is happy if it is at least D degrees
from any of the rest. You are to calculate how much time in a day that all the hands are happy.

 

[align=left]Input[/align]
The input contains many test cases. Each of them has a single line with a real number D between 0 and 120, inclusively. The input is terminated with a D of -1.

 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

0
120
90
-1

 
[align=left]Sample Output[/align]

100.000
0.000
6.251

[align=left]Author[/align]
PAN, Minghao

[align=left]Source[/align]
ZJCPC2004
 
[align=left]Recommend[/align]
JGShining
 
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct qujian
{
double l,r;
};
double D;
qujian solve(double a,double b)//解方程 D<=a*x+b<=360-D ,并且和 [0,60] 取交集
{
qujian p;
if(a>0)
{
p.l=(D-b)/a;
p.r=(360-D-b)/a;
}
else
{
p.l=(360-D-b)/a;
p.r=(D-b)/a;
}
if(p.l<0)p.l=0;
if(p.r>60)p.r=60;
if(p.l>=p.r)  p.l=p.r=0;
return p;
}
qujian jiao(qujian a,qujian b)
{
qujian p;
p.l=max(a.l,b.l);
p.r=min(a.r,b.r);
if(p.l>=p.r) p.l=p.r=0;
return p;
}
/*
hh=30*h+m/2+s/120;
mm=6*m+s/10;
ss=6*s;
D<=|hh-mm|<=360-D;
D<=|hh-ss|<=360-D;
D<=|mm-ss|<=360-D;
hh-mm=
*/
double happytime(int h,int m)//计算 h 时,m 分 满足题意的秒数
{
double a,b;
qujian s[3][2];
qujian s1;

//解方程 D<=|hh-mm|<=360-D
//hh=30*h+m/2+s/120;
//mm=6*m+s/10;
a=1.0/120-0.1;
b=30*h+m/2.0-6*m;
s[0][0]=solve(a,b);
s[0][1]=solve(-a,-b);

//解方程  D<=|hh-ss|<=360-D
//hh=30*h+m/2+s/120;
//ss=6*s;
a=1.0/120-6.0;
b=30*h+m/2.0;
s[1][0]=solve(a,b);
s[1][1]=solve(-a,-b);

//解方程  D<=|mm-ss|<=360-D
//mm=6*m+s/10;
//ss=6*s;
a=0.1-6;
b=6*m;
s[2][0]=solve(a,b);
s[2][1]=solve(-a,-b);

double res=0;
//六个区间,选三个取交集。
//因为绝对值的式子得到的两个区间要并,而三个不同表达式的区间要交,故这样做。
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
{
s1=jiao(jiao(s[0][i],s[1][j]),s[2][k]);
res+=s1.r-s1.l;
}
return res;
}

int main()
{
while(scanf("%lf",&D))
{
if(D==-1) break;
double res=0;
int h,m;
for(h=0;h<12;h++)
{
for(m=0;m<60;m++)
{
res+=happytime(h,m);
}
}
printf("%.3lf\n",res*100.0/(60*60*12));
}
return 0;
}

转:http://blog.sina.com.cn/s/blog_81650692010138nr.html
http://acm.hdu.edu.cn/showproblem.php?pid=1006

这是篇简单的小学数学题。但是因为水平不够,还是做了好久。不过我也终于明白了为什么总有人写解题报告。

——因为费了这么大的事才做出来的,当然要纪念下啦

步骤:

列出时间(h:m:s)与度数(rh:rm:rs)之间的方程:

rs=6*s;          rm=6*m+s/10;           rh=30*h+0.5*m+s/120;
各针之间的角度如下:

rm-rs=6*m+(0.1-6)*s;       rh-rs=30*h+0.5*m+(1/120)-6)*s;       rh-rm=30*h+(0.5-6)*m+((1/120)-0.1)*s;

指针间的度数要在d到360-d之间,即解三个|ax+b|型的不等式:(s为唯一未知数)

可以求出任意一分钟内的秒针取值范围,然后每分钟都求一遍。

#include <stdio.h>
#include <stdlib.h>
struct set
{
double a,b;
};
double d;
struct set sloveset(double a,double b);         / * 求 d<=ax+b<360-d 的解 */
struct set intersection(struct set a,struct set b);          / * 给两个集合取交集 */
int main()
{
int h,m,i,j,k;
double a1,b1,a2,b2,a3,b3,time;
struct set answer[3][2],ensemble;
while(scanf("%lf",&d)&&d!=-1)
{
time=0;
for(h=0; h<12; h++)
{
for(m=0; m<60; m++)
{
b1=6.0*m;                a1=-5.9;
b2=30*h+0.5*m;            a2=1.0/120-6.0;
b3=30*h+(0.5-6)*m;         a3=(1.0/120)-0.1;

/ * 求3个绝对值不等式的解集 存到answer中answer[0][0] answer[0][1]要取并集剩下两个也是 */
answer[0][0]=sloveset(a1,b1);              answer[0][1]=sloveset(-a1,-b1);
answer[1][0]=sloveset(a2,b2);              answer[1][1]=sloveset(-a2,-b2);
answer[2][0]=sloveset(a3,b3);              answer[2][1]=sloveset(-a3,-b3);

/ * 取过交集后,需要将3个式子的结果取并集 所以采用下面的方法

循环的意思就是红黄绿中各取一个求交集(上图表示数组answer)*/
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
ensemble=intersection(intersection(answer[0][i],answer[1][j]),answer[2][k]);
time+=ensemble.b-ensemble.a; } } }//地方不够了挤到一起啦
}
}
time=time*100.0/(12*3600);
printf("%.3lf\n",time);
}
return 0;
}

struct set sloveset(double a,double b)
{
struct set seta;
if(a>0)
{
seta.a=(d-b)/a;
seta.b=(360-d-b)/a;
}
else
{
seta.b=(d-b)/a;
seta.a=(360-d-b)/a;
}
if(seta.a<0) seta.a=0;
if(seta.b>60) seta.b=60;
if(seta.a>=seta.b) seta.a=seta.b=0; / /之前这句放到了if(seta.a<0)if(seta.b>60)前面了

return seta;              //结果seta.b变成了负的怀疑是seta.b太大了 冒了 不知对错
}
struct set intersection(struct set a,struct set b)
{
struct set p;
p.a=a.a>b.a ?a.a:b.a;
p.b=a.b<b.b ?a.b:b.b;
if(p.a>p.b) p.a=p.b=0;
return p;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: