模拟 HDOJ 5387 Clock

题目传送门

 1 /*
 2     模拟:这题没啥好说的,把指针转成角度处理就行了,有两个注意点:结果化简且在0~180内;小时13点以后和1以后是一样的(24小时)
 3         模拟题伤不起!计算公式在代码内(格式:hh/120, mm/120, ss/120)
 4 */
 5 /************************************************
 6 * Author        :Running_Time
 7 * Created Time  :2015-8-13 13:04:31
 8 * File Name     :H.cpp
 9  ************************************************/
10 
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29 
30 #define lson l, mid, rt << 1
31 #define rson mid + 1, r, rt << 1 | 1
32 typedef long long ll;
33 const int MAXN = 1e5 + 10;
34 const int INF = 0x3f3f3f3f;
35 const int MOD = 1e9 + 7;
36 char str[11];
37 
38 int GCD(int a, int b)   {
39     return (b == 0) ? a : GCD (b, a % b);
40 }
41 
42 int main(void)    {     //HDOJ 5387 Clock
43     int T;  scanf ("%d", &T);
44     while (T--) {
45         int h = 0, m = 0, s = 0;
46         int hh[2], mm[2], ss[2];
47         scanf ("%s", str + 1);
48         h = (h + (str[1] - '0')) * 10 + str[2] - '0';
49         m = (m + (str[4] - '0')) * 10 + str[5] - '0';
50         s = (s + (str[7] - '0')) * 10 + str[8] - '0';
51 
52         if (h >= 12)    h -= 12;        //WA点
53 
54         hh[0] = 3600 * h + 60 * m + s;      //角度
55         mm[0] = 720 * m + 12 * s;
56         ss[0] = 720 * s;
57 
58         int tmp = hh[0];                    //计算角度差,要在0~180度内
59         hh[0] = abs (hh[0] - mm[0]);
60         hh[0] = min (hh[0], 360*120 - hh[0]);
61         int tmp2 = mm[0];
62         mm[0] = abs (tmp - ss[0]);
63         mm[0] = min (mm[0], 360*120 - mm[0]);
64         ss[0] = abs (tmp2 - ss[0]);
65         ss[0] = min (ss[0], 360*120 - ss[0]);
66         
67         hh[1] = GCD (120, hh[0]);           //求GCD,化简
68         mm[1] = GCD (120, mm[0]);
69         ss[1] = GCD (120, ss[0]);
70 
71         if (hh[0] % 120 == 0) {             //输出
72             printf ("%d ", hh[0] / 120);
73         }
74         else   {
75             printf ("%d/%d ", hh[0] / hh[1], 120 / hh[1]);
76         }
77         if (mm[0] % 120 == 0) {
78             printf ("%d ", mm[0] / 120);
79         }
80         else   {
81             printf ("%d/%d ", mm[0] / mm[1], 120 / mm[1]);
82         }
83         if (ss[0] % 120 == 0) {
84             printf ("%d 
", ss[0] / 120);
85         }
86         else   {
87             printf ("%d/%d 
", ss[0] / ss[1], 120 / ss[1]);
88         }
89     }
90 
91     return 0;
92 }
编译人生,运行世界!
原文地址:https://www.cnblogs.com/Running-Time/p/4728356.html