hdu 1006

Tick and Tick

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


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


题解:

由于要记录连续时间,那么就利用区间记录,[begin,end]。另外,就拿时针和分针来做例子吧。时针的角速度为w_h=360.0/12*60*60=1.0/120,分针的角速度为w_m=360.0/60*60=1.0/10。两者的相对角速度为w_hm=w_m-w_h,“相对周期”为T_hm=360.0/w_hm。所谓的相对周期就是时针和分针出现重复的相对关系的最小时间。

这样的话就可以把时针和分针,时针和秒针,分针和秒针各自满足条件的集合求交集。显然,代码中利用的是三个for循环来暴力解决。不过有些显然不满足条件的情况即可去除,以减少计算次数。

x[3]和y[3]记录的是第一个开始满足条件的时间和第一个开始不满足条件的时间。m[3],n[3]则是分表根据相对周期来扩大x[3],y[3]以获得所有满足条件的时间集合,并求交集。

AC代码:

#include <iostream>  
#include <stdio.h>
#include <string>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iomanip>
using namespace std;
double hm = 11.0 / 120, hs = 719.0 / 120, sm = 59.0 / 10;//相对角速度
double thm = 43200.0 / 11, ths = 43200.0 / 719, tsm = 3600.0 / 59;//相对角周期
double max(double a, double b, double c)
{
	double x[3];
	x[0] = a, x[1] = b, x[2] = c;
	sort(x, x + 3);
	return x[2];
}
double min(double a, double b, double c)
{
	double x[3];
	x[0] = a, x[1] = b, x[2] = c;
	sort(x, x + 3);
	return x[0];
}
int main()
{
	double degree;
	double x[3], y[3];
	double m[3], n[3];
	double begin, end, sum;
	while (cin>>degree && degree != -1)
	{
		//第一个各集合满足的时间
		x[0] = degree / hm;
		x[1] = degree / hs;
		x[2] = degree / sm;
		//第一个各集合不满足的时间
		y[0] = (360 - degree) / hm;
		y[1] = (360 - degree) / hs;
		y[2] = (360 - degree) / sm;
		sum = 0.0;
		for (m[0] = x[0], n[0] = y[0]; n[0] <= 43200.000001; m[0] += thm, n[0] += thm)
			for (m[1] = x[1], n[1] = y[1]; n[1] <= 43200.000001; m[1] += ths, n[1] += ths)
			{
				if (n[0] < m[1])//第一个末时间小于第二个开始时间
					break;
				if (m[0] > n[1])//第一个开始时间大于第二个末时间
					continue;
				for (m[2] = x[2], n[2] = y[2]; n[2] <= 43200.000001; m[2] += tsm, n[2] += tsm)
				{
					if (n[0] < m[2] || n[1] < m[2])//类似上面判断
						break;
					if (m[0] > n[2] || m[1]>n[2])
						continue;
					begin = max(m[0], m[1], m[2]);
					end = min(n[0], n[1], n[2]);
					if (end > begin)
					{
						sum += end - begin;
					}
				}
			}
		printf("%.3lf\n", sum*100.0 / 43200);//求百分比
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值