hud 1006( Tick and Tick)

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

写这个题肝了我好几个小时, 自己写的精度有问题,一直差一点精度, 后来发现很多人也入坑了哈哈。

解释下这个题,把时间的长度想象成一个数轴(单位是秒),那么我们如果可以求出全部满足题目的区间, 那么用这些区间的长度和与时间轴的总长度一比就能得出答案。
问题是我们怎么才能求出满足题目要求的所有区间呢?
首先我们先求出每一个指针的角速度,求出角速度之差。那么我们就可以求出两个指针什么时候能够达到题目给定的角度,以及指针什么时候转一圈回来(求出周期的作用就是算一下这一天可以转多少圈)。我们有三个指针, 所以要求出满足三种的条件即可~,

#include <iostream>// 用了700+ 毫秒, 险过~
#include <algorithm>

using namespace std;

const double hs = 719.0 / 120, hm = 11.0 / 120, sm = 59.0 / 10;
const double Ths = 43200.0 / 719, Thm = 43200.0 / 11, Tsm = 3600.0 / 59;
int main()
{
	double t;

	double a[3], b[3];
	while (cin >> t && t != -1)
	{
		a[0] = t / hm;
		a[1] = t / sm;
		a[2] = t / hs;

		b[0] = (360 - t) / hm;
		b[1] = (360 - t) / sm;
		b[2] = (360 - t) / hs;

		double res = 0;
		double m[3], n[3];
		for (m[0] = a[0], n[0] = b[0]; n[0] <= 43200.0001; m[0] += Thm, n[0] += Thm)
		{
			for (m[1] = a[1], n[1] = b[1]; n[1] <= 43200.0001; m[1] += Tsm, n[1] += Tsm)
			{
				if (m[1] > n[0]) break;
				for (m[2] = a[2], n[2] = b[2]; n[2] <= 43200.0001; m[2] += Ths, n[2] += Ths)
				{
					if (m[2] > n[1] || m[2] > n[0]) break;

					double begin = max(m[0], max(m[1], m[2]));

					double end = min(n[0], min(n[1], n[2]));


					if (begin < end) res += (end - begin);
				}

			}
		}
		//cout << res << endl;
		printf("%.3lf\n", res / 43200 * 100);
	}
	return 0;
}
发布了53 篇原创文章 · 获赞 14 · 访问量 1892

猜你喜欢

转载自blog.csdn.net/weixin_45630535/article/details/104806935