算法笔记(入门篇1-入门模拟)--日期处理--问题 A: 日期差值

问题 A: 日期差值

时间限制: 1 Sec  内存限制: 32 MB

题目描述

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

输入

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

输出

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

样例输入

20130101
20130105

样例输出

5
#include<stdio.h>
int month[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}
};
bool isLeap(int year)
{
    return (year%4==0&&year%100!=0)||(year%400==0);
}
int main()
{
    int time1,y1,m1,d1;
    int time2,y2,m2,d2;
    int ans,i;
    while(scanf("%d%d",&time1,&time2)!=EOF)
    {
        ans=1;
        if(time1>time2)
        {
            int temp=time1;
            time1=time2;
            time2=temp;
        }
        y1=time1/10000;
        m1=time1%10000/100;
        d1=time1%100;
        y2=time2/10000;
        m2=time2%10000/100;
        d2=time2%100;
        while(y1<y2||m1<m2||d1<d2)
        {
            for(i=y1; i<y2-1; i++)//先把年份加上   有利于加快速度   年份只能算到y2-1  因为有可能不到一年
            {
                if(isLeap(i)==true)
                {
                    ans+=366;
                }
                else
                {
                    ans+=365;
                }
            }
            y1=i;
            d1++;
            if(d1==month[m1][isLeap(y1)]+1)
            {
                m1++;
                d1=1;
            }
            if(m1==13)
            {
                y1++;
                m1=1;
            }
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/syd1091245120/article/details/81415076