Topic 1096: Date Difference value
time limit:1 seconds
Memory limit:32 MB
Special question: No
submitted:5143
Resolution:1789
-
Title Description:
-
There are two dates, ask two days between dates, if two dates are contiguous we stipulate that the number of days between them is two days
-
Input:
-
There are more than one set of data, each set of data has two lines, representing two dates, in the form of YYYYMMDD
-
Output:
-
One row for each set of data, that is, the date difference value
-
Sample input:
-
2011041220110422
-
Sample output:
-
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]= ' + '; Y1=atoi (t); strncpy (t,s1+4,2); t[2]= ' + '; M1=atoi (t); strncpy (t,s1+6,2); t[2]= ' + '; D1=atoi (t); strncpy (t,s2,4); t[4]= ' + '; Y2=atoi (t); strncpy (t,s2+4,2); t[2]= ' + '; M2=atoi (t); strncpy (t,s2+6,2); t[2]= ' + '; D2=atoi (t); printf ("%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 Resu lt:accepted time:0 Ms memory:920 kb****************************************************************/
Nine degrees OJ 1096 Date difference value