則再將時針指向的位置的度數去跟分針指向的位置的度數進行絕對值相減,
即可得解。
(若大於180度,就利用360去減其值的絕對值去把它減到小於180度為止)
P.S. 時針指的刻度算法: 小時*30(一個小時30度) + 分/60 * 30(因為分針走一圈,時針就走30度)
分針指的刻度算法: 分*6(五分鐘走30度,則一分鐘走6度)
[C++](12ms, 762KB)
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> | |
#include<cmath> | |
using namespace std; | |
int main() | |
{ | |
int H, M; | |
char recycle; | |
while( cin >> H >> recycle >> M ) | |
{ | |
if( H == 0 && M == 0 ) | |
break; | |
double H_angle = static_cast<double>(H) * 30.0 + static_cast<double>(M) / 60.0 * 30.0; | |
double M_angle = static_cast<double>(M) * 6.0; | |
double angle = fabs( H_angle-M_angle ); | |
while( angle > 180 ) | |
angle = fabs( 360 - angle ); | |
cout << fixed << setprecision(3) << angle << endl; | |
} | |
return 0; | |
} |
0 意見:
張貼留言