上海交大2009 日期差值

题目描述:

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

输入:

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

输出:

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

样例输入:
20110412
20110422
样例输出:
11
这个题做了我一个晚上,不是不会做,说出来全是泪呀。。在定义月份的时候把11月份写成了31天,把12月份写成了30天,然后各种改。。。

不细心是种病,得治。。。一个晚上的时间呀。。。

#include<iostream>
#include<algorithm>
#include<cstdio>
int Abs(int x)
{
    return x < 0 ? -x : x;
}
using namespace std;
#define isyear(x) ((x%100!=0 && x%4==0) || (x%400==0 ))?1:0
//bool isyear(int x)
//{
//    if ((x % 100 != 0 && x % 4 == 0) || x % 400 == 0)
//        return true;
//    return false;
//}
int daysOfMonth[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}
};
struct date
{
    int day;
    int month;
    int year;
    void nextday()//计算下一天的日期
    {
        day++;
        if (day > daysOfMonth[month][isyear(year)])//若天数超过了当月最大的天数
        {
            day = 1;
            month++;//进入下一月
            if (month > 12)
            {
                month = 1;
                year++;//进入下一年
            }
        }

    }
};
int buf[5001][13][32];//保留预处理的天数
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("D:\\in.txt", "r", stdin);
    freopen("D:\\out.txt", "w", stdout);
#endif
    date tmp;
    int cnt = 0;
    tmp.day = 1;
    tmp.month = 1;
    tmp.year = 0;//初始化日期为0年1月1日
    while (tmp.year != 5001)
    {
        buf[tmp.year][tmp.month][tmp.day] = cnt;//将该日与0年1月1日的差值保存起来
        tmp.nextday();
        cnt++;
    }
    int y1, m1, d1;
    int y2, m2, d2;
    while (scanf("%4d%2d%2d", &y1, &m1, &d1) != EOF)
    {
        scanf("%4d%2d%2d", &y2, &m2, &d2);
        printf("%d\n", Abs(buf[y2][m2][d2] - buf[y1][m1][d1]) + 1);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值