计算 两日期 间隔天数 zzuli1878

本文介绍了一个使用C++实现的闰年判断和日期计算算法,通过输入年月日,输出该日期为当年的第几天。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<algorithm>
using namespace std;
#define N 1010000
#define INF 0x3f3f3f3f

int yearday[N];

int IsLeap(int year) ///判断闰年
{
    return ((year%4==0&&year%100!=0) || year%400==0);
}

void init()
{
    yearday[0]=0;
    for(int i=1; i<=1000000; i++)
        yearday[i]=yearday[i-1]+(IsLeap(i-1)==1 ? 366 : 365);
}

int Day(int y,int m,int d) ///判断该天是当年的第多少天。
{
    int k,leap;
    int tab[2][13]={{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};

    leap=IsLeap(y);

    for(k=1;k<m;k++)
    {
        d=d+tab[leap][k];
    }

    return d;
}

int main()
{
    int T, y1, m1, d1, y2, m2, d2, sum;

    init();
    scanf("%d", &T);

    while(T--)
    {
        scanf("%d%d%d%d%d%d", &y1, &m1, &d1, &y2, &m2, &d2);
        sum=0;

        sum+=yearday[y2]-yearday[y1];
        sum-=Day(y1,m1,d1);
        sum+=Day(y2,m2,d2);
        printf("%d\n", sum);

    }
    return 0;
}

转载于:https://www.cnblogs.com/liuLY/p/5407696.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值