九度OJ 1096 日期差值,九度oj1096差值
題目1096:日期差值
時間限制:1 秒
記憶體限制:32 兆
特殊判題:否
提交:5143
解決:1789
-
題目描述:
-
有兩個日期,求兩個日期之間的天數,如果兩個日期是連續的我們規定他們之間的天數為兩天
-
輸入:
-
有多組資料,每組資料有兩行,分別表示兩個日期,形式為YYYYMMDD
-
輸出:
-
每組資料輸出一行,即日期差值
-
範例輸入:
-
2011041220110422
-
範例輸出:
-
11
-
#include<stdio.h>#include<string.h>#include<stdlib.h>int months[]={0,31,28,31,30,31,30,31,31,30,31,30,31};int isLeapYear(int y){ if((y%4==0&&y%100!=0)||y%400==0)return 1; return 0;}int getdays(int y,int m,int d,int flag){ int result=0; if(isLeapYear(y)) { months[2]+=1; int i=1; while(i<m){ result+=months[i]; i++; } result+=d; months[2]-=1; if(flag==0) { return 366-result+1; } else return result; } int i=1; while(i<m){ result+=months[i]; i++; } result+=d; if(flag==0) { return 365-result+1; } else return result;} int main(int argc, char *argv[]){ int y1,m1,d1,y2,m2,d2; char s1[10]; char s2[10]; char t[10]; while(~scanf("%s%s",s1,s2)) { strncpy(t,s1,4); t[4]='\0'; y1=atoi(t); strncpy(t,s1+4,2); t[2]='\0'; m1=atoi(t); strncpy(t,s1+6,2); t[2]='\0'; d1=atoi(t); strncpy(t,s2,4); t[4]='\0'; y2=atoi(t); strncpy(t,s2+4,2); t[2]='\0'; m2=atoi(t); strncpy(t,s2+6,2); t[2]='\0'; d2=atoi(t); //printf("%d %d %d ---- %d %d %d\n",y1,m1,d1,y2,m2,d2); if(y1!=y2) { int re=getdays(y1 , m1 , d1 , 0); for(int i=y1+1;i<y2;++i) { if(isLeapYear(i)) re+=366; else re+=365; } re+=getdays(y2, m2 , d2 ,1); printf("%d\n",re); } else printf("%d\n",getdays(y2 , m2 , d2 , 1)-getdays(y1 , m1 , d1 , 1)+1); } return 0;} /************************************************************** Problem: 1096 User: kirchhoff Language: C Result: Accepted Time:0 ms Memory:920 kb****************************************************************/