【HDOJ】2395 Alarm Clock

水题。

 1 /* 2395 */
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 
 6 #define MAXL 20
 7 
 8 char src[MAXL], des[MAXL];
 9 
10 int min(int a, int b) {
11     return a<b ? a:b;
12 }
13 
14 int abs(int x) {
15     return x<0 ? -x:x;
16 }
17 
18 int main() {
19     int t;
20     int sh, sm1, sm0;
21     int dh, dm1, dm0;
22     int i, j, k, tmp;
23     int ans;
24     
25     #ifndef ONLINE_JUDGE
26         freopen("data.in", "r", stdin);
27     #endif
28     
29     scanf("%d", &t);
30     while (t--) {
31         scanf("%s %s", src, des);
32         ans = 0;
33         
34         // handle source
35         k = strchr(src, ':') - src;
36         sh = 0;
37         for (i=0; i<k; ++i)
38             sh = 10*sh + src[i]-'0';
39         sm1 = src[k+1] - '0';
40         sm0 = src[k+2] - '0';
41         
42         // handle des
43         j = strchr(des, ':') - des;
44         dh = 0;
45         for (i=0; i<j; ++i)
46             dh = 10*dh + des[i]-'0';
47         dm1 = des[j+1] - '0';
48         dm0 = des[j+2] - '0';
49         
50         // calculate ans
51         if (des[j+3] != src[k+3])
52             ++ans;
53         ans += min(abs(sm0-dm0), 10-abs(sm0-dm0));
54         ans += min(abs(sm1-dm1), 6-abs(sm1-dm1));
55         ans += min(abs(sh-dh), 12-abs(sh-dh));
56         
57         if (ans == 1)
58             printf("Going from %s to %s requires %d push.\n", src, des, ans);
59         else
60             printf("Going from %s to %s requires %d pushes.\n", src, des, ans);
61     }
62     
63     return 0;
64 }

 

posted on 2014-12-27 23:26  Bombe  阅读(210)  评论(0编辑  收藏  举报

导航