- 题目描述:
-
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天
- 输入:
-
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
- 输出:
-
每组数据输出一行,即日期差值
- 样例输入:
-
20110412 20110422
- 样例输出:
-
11
代码如下:#include "stdio.h" int dayOfMonth[13][2]={ 0,0, 31,31, 28,29, 31,31, 30,30, 31,31, 30,30, 31,31, 31,31, 30,30, 31,31, 30,30, 31,31 }; typedef struct Date{ int year; int month; int day; }Date; int isRunNian(int year){ //判断是否是闰年 if((year%100!=0&&year%4==0)||(year%400==0)) return 1; else return 0; } int compare(Date d1,Date d2){ if(d1.year>d2.year) return 1; else if(d1.year<d2.year) return 0; else{ if(d1.month>d2.month) return 1; else if(d1.month<d2.month) return 0; else{ if(d1.day>d2.day) return 1; else if(d1.day<d2.day) return 0; else return 2;//相同日期 } } } void nextDay(Date* date){ //计算下一天 (*date).day++; if((*date).day>dayOfMonth[(*date).month][isRunNian((*date).year)]){ (*date).day=1; (*date).month++; if((*date).month>12){ (*date).month=1; (*date).year++; } } } int main(int argc, char* argv[]) { Date date1,date2; while(scanf("%4d%2d%2d",&date1.year,&date1.month,&date1.day)!=EOF){ scanf("%4d%2d%2d",&date2.year,&date2.month,&date2.day); //printf("%d ",compare(date1,date2)); //printf("date1.year:%d date1.month:%d date1.day:%d ",date1.year,date1.month,date1.day); //printf("date2.year:%d date2.month:%d date2.day:%d ",date2.year,date2.month,date2.day); Date big_Date,small_Date; if(compare(date1,date2)==1){ big_Date.year=date1.year; big_Date.month=date1.month; big_Date.day=date1.day; small_Date.year=date2.year; small_Date.month=date2.month; small_Date.day=date2.day; } else{ big_Date.year=date2.year; big_Date.month=date2.month; big_Date.day=date2.day; small_Date.year=date1.year; small_Date.month=date1.month; small_Date.day=date1.day; } int count=1;//用于计数 while(compare(small_Date,big_Date)==0||compare(small_Date,big_Date)==1){//小日期追赶大日期,count计数 count++; nextDay(&small_Date); } printf("%d ",count); } return 0; } /************************************************************** Problem: 1096 User: lcyvino Language: C Result: Accepted Time:10 ms Memory:912 kb ****************************************************************/