HDU online 1006 Tick and Tick

Tick and Tick

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20859    Accepted Submission(s): 5470


php

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.
 
Sample Input
0 120 90 -1
 
Sample Output
100.000 0.000 6.251
 
Author
PAN, Minghao
 
Source
 
 
起初对于这道题,思路是对于每一秒都进行一个判断,在12个小时内(一个周期)内进行一个评估,对于每一秒的三个指针进行角度计算,可是虽然实现起来比较简单,可是实现的结果精度上却不太够,毕竟一秒对于秒针来讲是走了6°,具体代码以下:
 
#include<iostream> 
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;


bool cmp(double i,double j,double distance){           //三根指针之间角度与给定度数之间的对比 
	if((i-j>=distance)&&(360-i+j>=distance)) return true;
	else if((j-i>=distance)&&(360-j+i>=distance)) return true;
	else return false;
}

bool check(double m,int i,int j,double k){
	
	double sec = k*6;                             //秒针一秒走了6度 
	double min = (double)j*6 + k/10;                //分针在当前时间走了 6*j + 秒针进位的度数 
	double h = 30*(double)i + (double)j/2 + k/120;   // 时针走的度数 取决于当前几点,几分,几秒 
	return (cmp(sec,min,m)&&cmp(sec,h,m)&&cmp(min,h,m));

}
int main(){
	double m;
	while(cin>>m&&m!=-1){  
		double cnt = 0.0;
		for(int i=0;i<12;i++){
			for(int j=0;j<60;j++){
				for(double k=0;k<60;k=k+0.02){    //对秒进行精度划分 
					if(check(m,i,j,k)) cnt++;
				}
			}
		} 
		cout<<setiosflags(ios::fixed)<<setprecision(3)<<100.0*2*cnt/(12*60*6000)<<endl;
	}
}

为了精度,设置了对秒针进行步数的细化,大概0.2秒进行一次判断能够达到精度,可是超时的,因而乎只能用计算的方法,而不是模拟的方法:ios

一、设置一个时针与分针的循环app

二、根据时针与分针以及给的夹角,时针与分针应保持角度下算出秒钟的范围this

三、根据时针以及给的夹角,时针与秒针保持角度下算出秒钟的范围spa

四、根据分针以及给的夹角,分针与秒针保持角度下算出秒钟的范围指针

五、三个范围取交集,累加求得答案。rest

代码以下:blog

相关文章
相关标签/搜索