1116: 日期差值

题目描述

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天

输入

有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD

输出

每组数据输出一行,即日期差值

样例输入
20130101
20130105
样例输出
5
 
来源
View Code
 1 #include<stdio.h>
 2 
 3 int isRun(int year) {
 4     if( ((year%4 ==0) && (year%100!=0)) || year%400 == 0) {
 5         return 1;
 6     }
 7     return 0;
 8 }
 9 int dayOfMonth[13][2] = {
10     0,0,
11     31,31,
12     28,29,
13     31,31,
14     30,30,
15     31,31,
16     30,30,
17     31,31,
18     31,31,
19     30,30,
20     31,31,
21     30,30,
22     31,31
23 };
24 
25 struct Date {
26     int year;
27     int month;
28     int day;
29 };
30 
31 void nextDay(struct Date *d) {
32         d->day++;
33         if(d->day > dayOfMonth[d->month][isRun(d->year)]) {
34             d->month++;
35             d->day = 1;
36         }
37         if(d->month > 12) {
38             d->year++;
39             d->month = 1;
40         }
41 }
42 
43 int abs(int x) {
44     if(x<0) return -x;
45     return x;
46 }
47 /* 这个buf数组太大,不能放在堆栈中,要放在全局变量这,即内存中的静态存储区*/
48 int buf[5005][13][32];  
49 int main() {
50 
51     int year1,month1,day1;
52     int year2,month2,day2;
53     struct Date d = {0,1,1};
54     int cha = 0;
55     while(d.year!=5001) {
56         
57         buf[d.year][d.month][d.day] = cha++;
58         nextDay(&d);
59     };
60 
61 
62     while(scanf("%4d%2d%2d",&year1,&month1,&day1)!=EOF) {
63         scanf("%4d%2d%2d",&year2,&month2,&day2);
64         printf("%d\n",abs(buf[year1][month1][day1]-buf[year2][month2][day2])+1);
65     }
66     
67     
68 }

 

posted @ 2013-03-01 16:00  msober  阅读(126)  评论(0编辑  收藏  举报