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

相關文章
相關標籤/搜索