HDU 1006 Tick and Tick(模拟)

版权声明:本文为博主原创文章,欢迎转载。如有问题,欢迎指正。 https://blog.csdn.net/weixin_42172261/article/details/88376735

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.

看了半天好多人的题解才明白,原来好多人都是一开始把数据离散化处理了。
可以用角速度来求解,计算每次三个指针刚满足条件到刚不满足条件所经历的时间段就好
一天24小时,时针转两圈,两次的情况一样,所以只算一部分满足条件的时间除以12个小时就可以
其实到现在我也不太明白,(虽然看了一上午题解)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
 
//sh为秒针时针1s内相差多少度,sm为秒针分针,mh为分针时针 
const double sh=719.0/120, sm=59.0/10, mh=11.0/120;
//t_sh,t_sm,t_mh为相差360度所用时间 
const double t_sh=43200.0/719, t_sm=3600.0/59, t_mh=43200.0/11; 

double getmin(double a, double b, double c)
{
	return min(a, min(b, c));
} 
double getmax(double a, double b, double c)
{
	return max(a, max(b, c));
}

int main()
{
	double D;
	while (scanf("%lf", &D)!=EOF && D!=-1){
		double begin_sh=D/sh;//相差D度所用时间 
		double begin_sm=D/sm;//即从重合到相差D度时间 
		double begin_mh=D/mh;
		
		double end_sh=(360-D)/sh;//从开始到不满足相差D度时间 
		double end_sm=(360-D)/sm;
		double end_mh=(360-D)/mh; 
		
		double Begin, End, Sum=0;
		//三重循环的sh,sm,mh顺序随便 
		for (double b3=begin_sh, e3=end_sh; e3<=43200.000001; b3+=t_sh, e3+=t_sh){
			for (double b2=begin_sm, e2=end_sm; e2<=43200.000001; b2+=t_sm, e2+=t_sm){
				if (e2<b3)	
					continue;
				if (b2>e3)
					break;
				for (double b1=begin_mh, e1=end_mh; e1<=43200.000001; b1+=t_mh, e1+=t_mh){
					if (e1<b2 || e1<b3)
						continue;
					if (b1>e2 || b1>e3)
						break;
					Begin=getmax(b1, b2, b3);
					End=getmin(e1, e2, e3);
					Sum+=(End-Begin);
				}
			}
		} 
		printf("%.3lf\n", Sum/432);//除以43200×100% 
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42172261/article/details/88376735