2017年10月15日 星期日

[C_MM052-易] 垂直的時分針(C++)

[C_MM052-易] 垂直的時分針

問題描述 :
有一時鐘僅有分針與時針。請找出某個時段中,時針與分針會幾乎呈現垂直狀態的時刻。幾乎垂直的意思是時分針的夾角介於88度~92度之間都算。
時段的輸入為小時制,共兩個輸入整數,分別代表幾點開始及幾點終止。
該時鐘的分針與時針移動的精確度到1分鐘;亦即,在1小時時段中,分針會走60步,時針亦會走60步,但兩者每步所經過的角度不同。
請注意,不管時針與分針位於何處,其夾角定義在0~180度之間。例如: 11:50的夾角是55度,而非305度; 00:35的夾角是167.5度,而非192.5度。
輸入說明 :
輸入行包含兩個整數,第2個比第1個大.
第1個整數:代表開始時數的整數值,可為0~24。
第2個整數:代表終止時數的整數值,可為1~24。
輸出說明 :
所有符合的時刻及其精確的時分針夾角。
輸出格式: hh:mm xx.xx
其中, hh為小時,個位數前需補0; mm為分鐘,個位數前需補0; xx.xx為浮點數,取小數點兩位,兩者之間以一個空白隔開。
範例 :

Sample Input:Sample Output:
12 15
12:16 degree=88.00
12:49 degree=90.50
13:22 degree=91.00
14:27 degree=88.50
15:00 degree=90.00
0 3
00:16 degree=88.00
00:49 degree=90.50
01:22 degree=91.00
02:27 degree=88.50
03:00 degree=90.00





  1. #include <iostream>  
  2. #include <cmath>  
  3. #include<iomanip>  
  4. using namespace std;  
  5.   
  6. int main() {  
  7.     // [C_MM052-易] 垂直的時分針  
  8.     int s,e;  
  9.     double w_h,w_bh, h, min;  
  10.     double degree;  
  11.     while(cin >> s >> e)  //輸入起始和終止時間
  12.     {  
  13.         if(e > s && s >= 0 && s <= 24 && e > 0 && e <=24)  
  14.         //輸入行包含兩個整數,第2個比第1個大.
  15.              //第1個整數:代表開始時數的整數值,可為0~24。
  16.              //第2個整數:代表終止時數的整數值,可為1~24。
  17.         {  
  18.             for(int i = s; i < e;i++)  
  19.             {  
  20.                 w_h = (i%12)*30;         //3:00,9:00
  21.                 w_bh = 360-((i%12)*30);  //15:00, 21:00
  22.                 if(w_h >= 88 && w_h <= 92 && i != s)  
  23.                 //判斷整點時,垂直也只有3:00,9:00, 15:00, 21:00
  24.                 {  
  25.                     if(i < 10)  //個位數要補0
  26.                     {  
  27.                         cout << "0" << i << ":00 degree=" << fixed << setprecision(2) << (double)w_h <<endl ;  
  28.                     }  
  29.                     else  
  30.                     {  
  31.                         cout << i << ":00 degree=" << fixed << setprecision(2) << (double)w_h <<endl ;  
  32.                     }  
  33.                 }  
  34.                 if(w_bh >= 88 && w_bh <= 92 && i != s)  
  35.                 {  
  36.                     if(i < 10)  
  37.                     {  
  38.                         cout << "0" << i << ":00 degree=" << fixed << setprecision(2) << (double)w_bh <<endl ;  
  39.                     }  
  40.                     else  
  41.                     {  
  42.                         cout << i << ":00 degree=" << fixed << setprecision(2) << (double)w_bh <<endl ;  
  43.                     }  
  44.                 }  
  45.                   
  46.                 for(int j = 1;j < 60;j++)  
  47.                 {  
  48.                     //時針:i*30+0.5*j  
  49.                     //分針:6*j  
  50.                     h = (i%12)*30 + 0.5*j;  //時針
  51.                     min = 6*j;              //分針
  52.                     degree = h - min;       //角度
  53.                     if(degree < 0)          //轉成正數
  54.                     {  
  55.                         degree = degree * -1;  
  56.                     }  
  57.                     if(degree > 180)       
  58.                     {  
  59.                         if( h > 180 || min > 180)  
  60.                         {  
  61.                             if(min > 180)  
  62.                             {  
  63.                                 degree = 360 - min + h;  
  64.                             }  
  65.                             else if(h >180)  
  66.                             {  
  67.                                 degree = 360 - h + min;  
  68.                             }  
  69.                             else  
  70.                             {  
  71.                                     degree = h - min;  
  72.                             }  
  73.                         }  
  74.                         else  
  75.                         {  
  76.                             degree = h - min;  
  77.                         }  
  78.                     }  
  79.                     /*cout << i << ":" << j << " degree=" << degree <<endl ;*/  
  80.                     if(degree >= 88 && degree <= 92)  
  81.                     {  
  82.                         if(i < 10 && j < 10)  
  83.                         {  
  84.                             cout << "0" << i << ":" << "0" << j << " degree=" << fixed << setprecision(2) << degree <<endl;  
  85.                         }  
  86.                         else if(i < 10)  
  87.                         {  
  88.                             cout << "0" << i << ":" << j << " degree=" << fixed << setprecision(2) << degree <<endl;  
  89.                         }  
  90.                         else if(j < 10)  
  91.                         {  
  92.                             cout << i << ":" << "0" << j << " degree=" << fixed << setprecision(2) << degree <<endl;  
  93.                         }  
  94.                         else  
  95.                         {  
  96.                             cout << i << ":" << j << " degree=" << fixed << setprecision(2) << degree <<endl;  
  97.                         }  
  98.                     }  
  99.                 }  
  100.             }  
  101.             if(e < 12)  
  102.             {  
  103.                 if((e*30) >= 88 && (e*30) <= 92)  
  104.                 {  
  105.                     if(e < 10)  
  106.                     {  
  107.                         cout << "0" << e << ":00 degree=" << fixed << setprecision(2) << (double)e*30 <<endl ;  
  108.                     }  
  109.                     else  
  110.                     {  
  111.                         cout << e << ":00 degree=" << fixed << setprecision(2) << (double)e*30 <<endl ;  
  112.                     }  
  113.                 }  
  114.                 if(360-(e*30) >= 88 && 360-(e*30) <= 92)  
  115.                 {  
  116.                     if(e < 10)  
  117.                     {  
  118.                         cout << "0" << e << ":00 degree=" << fixed << setprecision(2) << (double)360-(e*30) <<endl ;  
  119.                     }  
  120.                     else  
  121.                     {  
  122.                         cout << e << ":00 degree=" << fixed << setprecision(2) << (double)360-(e*30) <<endl ;  
  123.                     }  
  124.                 }  
  125.             }  
  126.             else  
  127.             {  
  128.                 if(((e-12)*30) >= 88 && ((e-12)*30) <= 92)  
  129.                 {  
  130.                     if(e < 10)  
  131.                     {  
  132.                         cout << "0" << e << ":00 degree=" << fixed << setprecision(2) << (double)(e-12)*30 <<endl ;  
  133.                     }  
  134.                     else  
  135.                     {  
  136.                         cout << e << ":00 degree=" << fixed << setprecision(2) << (double)(e-12)*30 <<endl ;  
  137.                     }  
  138.                 }  
  139.                 if((360-((e-12)*30)) >= 88 && ((360-(e-12)*30)) <= 92 && e != s)  
  140.                 {  
  141.                     if(e < 10)  
  142.                     {  
  143.                         cout << "0" << e << ":00 degree=" << fixed << setprecision(2) << (double)(360-(e-12)*30) <<endl ;  
  144.                     }  
  145.                     else  
  146.                     {  
  147.                         cout << e << ":00 degree=" << fixed << setprecision(2) << (double)(360-(e-12)*30) <<endl ;  
  148.                     }  
  149.                 }  
  150.             }  
  151.         }  
  152.     }  
  153.     return 0;  
  154. }  

沒有留言:

張貼留言