UVa Volume V 579 - Clock Hands
題目來源 : Clock Hands
解題 :
此題在判斷某時的分針和時針角度差。
首先,資料輸入方面這次直接用scanf()來抓取資料省去麻煩。
接著,在處理角度差時,我處理方式如下 :
將hour乘以30︑minute乘以6來換算成角度。
需要注意的是,當分針不是指向00時候,還需要將時針加上些微的移動。
因為每個小時的角度為30度,所以將30除以60(每個小時60分),
便可以得到 :
每過一分鐘時針移動角度為0.5度
最後整理出公式為 : h * 30 + m * 0.5 - m * 6
參考解答(C++)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
using namespace std; | |
int main(void) | |
{ | |
// 設定顯示至小數點第三位 | |
cout << setiosflags(ios::fixed) << setprecision(3); | |
while (1) | |
{ | |
int h, m; | |
scanf("%d:%d", &h, &m); | |
if (!h && !m) | |
{ | |
break; | |
} | |
// 分針每前進 1 度, 時針就前進 0.5 度 | |
double angle = (double)h * 30 - (double)m * 5.5; | |
if (angle < 0) | |
{ | |
angle *= -1; | |
} | |
if (angle > 180) | |
{ | |
angle = 360 - angle; | |
} | |
cout << angle << endl; | |
} | |
system("pause"); | |
return 0; | |
} |
沒有留言:
張貼留言