【HDOJ】4515 小Q系列故事——世界上最遥远的距离

简单题目,先把时间都归到整年,然后再计算。同时为了防止减法出现xx月00日的情况,需要将d先多增加1,再恢复回来。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 
 5 int sy, sm, sd;
 6 int ay, am, ad;
 7 
 8 int map[2][13] = {
 9     {0,31,28,31,30,31,30,31,31,30,31,30,31},
10     {0,31,29,31,30,31,30,31,31,30,31,30,31},
11 };
12 
13 int isLeap(int n) {
14     if ( (n%4==0 && n%100!=0) || (n%400 == 0) )
15         return 366;
16     else
17         return 365;
18 }
19 
20 void subDate(int d) {
21     int i, j, k;
22     
23     sy = 2013;
24     d += 283;
25     while (d > isLeap(sy)) {
26         d -= isLeap(sy);
27         --sy;
28     }
29     i = isLeap(sy)==366 ? 1:0;
30     j = 12;
31     while (d > map[i][j]) {
32         d -= map[i][j];
33         --j;
34     }
35     sm = j;
36     sd = map[i][j] - d + 1;
37 }
38 
39 void addDate(int d) {
40     int i, j, k;
41     
42     ay = 2013;
43     d += 83;
44     while (d > isLeap(ay)) {
45         d -= isLeap(ay);
46         ++ay;
47     }
48     i = isLeap(ay)==366 ? 1:0;
49     j = 1;
50     while (d > map[i][j]) {
51         d -= map[i][j];
52         ++j;
53     }
54     am = j;
55     ad = d;
56 }
57 
58 int main() {
59     int t, d;
60     
61     #ifndef ONLINE_JUDGE
62         freopen("data.in", "r", stdin);
63         //freopen("data.in", "r", stdout);
64     #endif
65     
66     scanf("%d", &t);
67     while (t--) {
68         scanf("%d", &d);
69         subDate(d);
70         addDate(d);
71         printf("%04d/%02d/%02d %04d/%02d/%02d\n", ay,am,ad, sy,sm,sd);
72     }
73     
74     return 0;
75 }

 

posted on 2014-12-22 00:06  Bombe  阅读(190)  评论(0编辑  收藏  举报

导航