两日期间的间隔

#include <stdio.h>

int judge(int y){
    if((y%100==0&&y%400==0)||(y%100!=0&&y%4==0))
        return 1;
    return 0;
}

int which_day(int year,int mon,int day){
    int i,n,a[]={31,28,31,30,31,30,31,31,30,31,30,31};
    if(mon > 2 && judge(year))
          day += 1;
    for(n = i = 0;i < mon - 1; ++i)
        n += a[i];
    return n + day;
}

int main(){
    int y,m,d,y2,m2,d2,sum;
    scanf("%d-%d-%d",&y,&m,&d);
    scanf("%d-%d-%d",&y2,&m2,&d2);
    if(m==2&&d==29&&!judge(y2))
    {
        printf("Unavailable.\n");
        return 0;
    }
    sum=0;
    if(y2 > y){
        sum += 365 + judge(y) - which_day(y,m,d);
        sum += which_day(y2,m2,d2);
    }
    else if(y2 == y){
        sum = which_day(y2,m2,d2) - which_day(y,m,d);
    }
    for(int i=y+1;i<y2;i++)
    {
        sum += (365 + judge(i));
    }
    printf("%d\n",sum);
    return 0;
}

 

posted @ 2014-06-12 00:51  Naturain  阅读(353)  评论(0编辑  收藏  举报