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.

问题描述:

时钟上有三根指针(时针,分针,秒针)每秒都会转动,三个指针每天会相遇多次。最终,它们不开心了,每根指针都想与其他两根指针保持距离。一个指针与其他指针至少保持D度,它就开心,你需要做的是计算一天中多少时间它们是开心的。

输入:

输入包含许多例子,每一行各自包含一个数字D(范围是0~120),输入以输入D为-1的时候结束。

输出:

对每个输入的D,打印出指针嗨皮的时间占一天总时间的比例,结果精度三位数。

这个问题就是解决不等式问题,一个指针距离其他任何一个指针角度为D时,那么这个指针就是开心的,问题需要三个指针都需要开心,为了便于理解,我们先假设两根指针是开心的,即只要这两个指针相隔D度时便成立。而且在未来某个范围内都可以成立,这个范围,初始值,便是,两个指针相隔D度,结束就是360-D,因为我们看的时两个指针,所以不用考虑第三根时针。所以范围是:

                                                       【D,360-D】

现在我们只需要求出两个时针距离的角度就行了。

设时针在某个时间的角度为H,分针的角度为M,秒针的角度为S。

时针每秒转的度数是1.0/120,即时针转速是vh = 1.0/120 度/秒。

分针每秒转的度数是1.0/10,即分针的转速是 vm = 0.1 度/秒。

秒针每秒转的度数是6,即秒针的转速是vs = 6 度/秒。

所以在h时m分s秒时,各个指针的角度是:

H=30*h+m/2+s/120;

M=m*6+s/10;

S=6*s;

现在,只需要列出不等式就行,输入的相隔时间是D,只要两个指针的相隔角度差在【D,360-D】这个范围就行了。

D <= |H- M| <= 360-D
D <= |H - S| <= 360-D
D <= |M- S| <= 360-D

#include <iostream>
#include <algorithm>
#include <iomanip>

using namespace std;

double max(double a,double b,double c)
{
    double result = a>b?a:b;
    result = result>c?result:c;
    return result;
}


double min(double a,double b,double c)
{
    double result = a<b?a:b;
    result = result<c?result:c;
    return result;
}

int main()
{
    double D;
    while(cin>>D&&D!=-1)
	{
		double vsh,vsm,vmh,tsh,tsm,tmh,start,end,total=0;
		vsm = 6.0-1.0/10.0;
		vsh = 6.0-1.0/120.0;
		vmh = 1.0/10.-1.0/120.0;
		tsm = 360.0/vsm;
		tsh = 360.0/vsh;
		tmh = 360.0/vmh;
		double bsh,esh,bsm,esm,bmh,emh;
		bsh = D/vsh;
		esh = (360.0-D)/vsh;
		bsm = D/vsm;
		esm = (360.0-D)/vsm;
		bmh = D/vmh;
		emh = (360.0-D)/vmh;
		while(bmh<43200)
		{
			while(bsm<emh)
			{
				while(bsh<emh&&bsh<esm)
				{
					start = max(bsh,bsm,bmh);
					end = min(esh,esm,emh);
					total += (end-start)>0?(end-start):0;
					bsh = bsh+tsh;esh = esh+tsh;
				}
				bsh = bsh-tsh;esh = esh-tsh;
				bsm = bsm+tsm;esm = esm+tsm;
			}
			bsm = bsm-tsm;esm = esm-tsm;
			bmh = bmh+tmh;emh = emh+tmh;
		}
		cout<<fixed<<setprecision(3)<<total/432<<endl;
	}
    return 0;

}

                               算法-时钟问题-杭电1006

相关文章:

  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
  • 2021-07-04
  • 2021-06-19
  • 2021-12-10
  • 2022-01-10
  • 2021-12-14
猜你喜欢
  • 2022-12-23
  • 2022-01-28
  • 2022-01-07
  • 2021-08-08
  • 2022-02-04
  • 2021-09-26
相关资源
相似解决方案