HDOJ1006(滴答滴)

Tick and Tick

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


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


問題描述:

時鐘上有三根指針(時針,分針,秒針)每秒都會轉動,三個指針每天會相遇多次。最終,它們不開心了,每根指針都想與其他兩根指針保持距離。一個指針與其他指針至少保持D度,它就開心,你需要做的是計算一天中多少時間它們是開心的。

輸入:

輸入包含許多例子,每一行各自包含一個數字D(範圍是0~120),輸入以輸入D爲-1的時候結束。

輸出:

對每個輸入的D,打印出指針嗨皮的時間佔一天總時間的比例,結果精度三位數


分析:


代碼


#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
double vsm=59.0/10,vsh=719.0/120,vmh=11.0/120;//速度差值 度/秒
double tsm=360.0/vsm,tsh=360.0/vsh,tmh=360.0/vmh;//360度爲一個週期 一個週期的時間是多少 Q1:何時重合 相差360度時候
double b1,b2,b3;//開始時間
double e1,e2,e3;//結束時間
int k1,k2,k3;
double total,angle,start,ended;
int main(){
    while (cin>>angle,angle != -1){
            total=0;
            b3=angle / vmh;//相差角度是angle的時候經過的時間
            e3=(360-angle)/vmh;
            b1=angle/vsm;
            e1=(360-angle)/vsm;
            b2=angle/vsh;
            e2=(360-angle)/vsh;
            while(b3<43200){//12h*60min*60s
                while(b1<e3){
                    while(b2<e3&&b2<e1)
                    {
                      start=max(max(b1,b2),b3);
                      ended=min(min(e1,e2),e3);
                      total+=(ended-start>0)?ended-start:0;
                      b2=b2+tsh; e2=e2+tsh;//控制跳出已經完成一個週期
                    }
                    b2=b2-tsh;e2=e2-tsh;
                    b1=b1+tsm;e1=e1+tsm;
                }
                b1=b1-tsm;e1=e1-tsm;
               b3=b3+tmh; e3=e3+tmh;
            }
            cout<<fixed<<setprecision(3)<<total/432<<endl;//fixed是以固定點方式顯示,小數點後幾位,除非cout.unsetf( iOS::fixed );否則一直在
    }
    return 0;
}


結果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章