您的位置:首页 > 编程语言 > Java开发

杭电ACM1006 Tick and Tick

2015-11-30 19:42 309 查看
Problem Description

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.

Input

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.

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.

题目大意:给定一个角度D,求出一天当中时针与分针,分针与秒针,秒针与时针的角度都大于D的时间占一天总时间的百分比。

分析:我们知道钟表12个小时是一个周期,我们可以在12个小时内讨论。这道题如果搜索全天43200秒,来判断有多少秒符合条件,精度是达不到要求的,而且会出现错误答案,当然可以继续细化,搜索4320000毫秒,甚至更大,但是这样程序就会超时。我们可以换一种思路。

t秒时,秒针,时针,分针转过的角度为

s(t)=6t,m(t)=0.1t,h(t)=1/120*t.

要使时针与分针,分针与秒针,秒针与时针的角度都大于D,要满足以下条件:

D+360 * k1< m(t)-h(t)< 360 * (k1+1)-D, 0 =< k1< = 10;

D+360 * k2< s(t)-m(t)< 360 * (k2+1)-D, 0 =< k2 < = 707;

D+360 * k3< s(t)-h(t)< 360 * (k3+1)-D, 0 < = k3 < = 718;

现在我们来解释一下k1,k2,k3的范围是怎么回事。时针12小时转1圈,分针12小时转12圈,秒针十二小时转720圈,所以分针12小时内最多套时针11圈,故k1可以取0,1,2,…10这十一个值,同理可以理解k2,k3。

我们求出满足上述三个条件的t的区间的交集就可以了。

现在可以直接根据上面的条件来进行编程了。我这里是用java实现的。

import java.util.Scanner;

public class TickAndTick {

public static void main(String args[]){
Scanner s=new Scanner(System.in);
while(true){
double D=s.nextDouble();
if(D==-1)
break;
double mhmin[]=new double[11];
double mhmax[]=new double[11];
for(int i=0;i<11;i++){
mhmin[i]=(360*i+D)*120/11;
mhmax[i]=(360*i+360-D)*120/11;
}
double smmin[]=new double[708];
double smmax[]=new double[708];
for(int i=0;i<708;i++){
smmin[i]=(360*i+D)/5.9;
smmax[i]=(360*i+360-D)/5.9;
}
double shmin[]=new double[719];
double shmax[]=new double[719];
for(int i=0;i<719;i++){
shmin[i]=(360*i+D)*120/719;
shmax[i]=(360*i+360-D)*120/719;
}
double countTime=0;
for(int i=0;i<11;i++){
for(int j=0;j<708;j++){
if(smmin[j]<mhmax[i]&&smmax[j]>mhmin[i]){
for(int k=0;k<719;k++){
if(shmin[k]<smmax[j]&&shmax[k]>smmin[j]){
if(shmin[k]<mhmax[i]&&shmax[k]>mhmin[i]){
double min=Max(shmin[k],smmin[j],mhmin[i]);
double max=Min(shmax[k],smmax[j],mhmax[i]);
countTime=countTime+max-min;
}
}
}
}
}
}
double percent=countTime/432;
System.out.printf("%.3f", percent);
System.out.println();
//=============Error Method=====================
/*int count=0;
double f=Math.cos(D*Math.PI/180);
System.out.println(f);
for(int i=0;i<43200;i++){
if(Math.cos(5.9*i/180)<f){
if(Math.cos((0.1-1/120)*i*Math.PI/180)<f){
if(Math.cos((6-1/120)*i*Math.PI/180)<f){
count++;
System.out.println(i);
}
}
}
}
double r=((double)count)/432;
System.out.printf("%.3f", r);
System.out.println();*/
//================================================
}
}

private static double Min(double d, double e, double f) {
// TODO Auto-generated method stub
double min=0;
min=d>e?e:d;
min=min>f?f:min;
return min;
}

private static double Max(double d, double e, double f) {
// TODO Auto-generated method stub
double max=0;
max=d>e?d:e;
max=max>f?max:f;
return max;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  杭电 acm 1006 java