首页 > 试题广场 > 日期差值
[编程题]日期差值
  • 热度指数:6504 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天

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


输出描述:
每组数据输出一行,即日期差值
示例1

输入

20110412
20110422

输出

11

//计算两个日期距离0000年0月1日的天数再求差

#include <bits/stdc++.h>
using namespace std;
int mon[12]={0,31,59,90,120,151,181,212,243,273,304,334};//字典 
int cal(int y,int m,int d)//给出年月日,计算距离0000年0月1日的天数和
{
    return y*365+y/4-y/100+y/400+mon[m-1]+d-1+(y/100!=0&&y/4==0||y/400==0&&m>2);
}
int main()
{
    int x[2],year,month,day;
    for(int k=0;k<2;k++)//循环两次读入两个日期
    {
        scanf("%4d%2d%2d",&year,&month,&day);
        x[k]=cal(year,month,day);
    }
    cout<<abs(x[0]-x[1])+1;
}
编辑于 2019-01-23 17:35:37 回复(3)
#include<stdio.h>
#define leap(x) (x%400==0||(x%4==0&&x%100!=0)?1:0)
#define Abs(x) ((x)>0?(x):-(x))
typedef struct date
{
    int y, m, d;
}Date;
const int yd[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}};
                     
int days(Date x)
{
    int sum=0, i;
    int y=x.y, m=x.m, d=x.d;
    for(i=0; i<y; i++)
    {
        if(leap(i))
        {
            sum+=366;
        }
        else
        {
            sum+=365;
        }
    }
    for(i=1; i<m; i++)
    {
        sum+=yd[leap(y)][i];
    }
    sum+=d;
    return sum;
}
int main(void)
{
    Date a, b;
    while(scanf("%4d%2d%2d", &a.y, &a.m, &a.d)!=EOF)
    {
        scanf("%4d%2d%2d", &b.y, &b.m, &b.d);
        printf("%d\n", Abs(days(a)-days(b))+1);
    }
    return 0;
}

发表于 2018-03-10 15:36:09 回复(0)

python只需要一行代码搞定。

要借助datetime库。

from datetime import datetime
while True:
    try:
        print(-(datetime.strptime(input(),"%Y%m%d")-datetime.strptime(input(),"%Y%m%d")).days+1)
    except:
        break
编辑于 2018-01-27 16:12:49 回复(3)
#include <iostream>

using namespace std;

int md[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

// 日期结构体
struct Date {
    int y;
    int m;
    int d;
};

// 判断是否为闰年
bool isRun(int y) {
    return (y%4==0&&y%100!=0||y%400==0);
}

// 获取指定年份的月份的天数
int get_md(int y, int m) {
    if(isRun(y) && m == 2) {
        return 29;
    } else {
        return md[m];
    }
}

// 转化日期格式
Date get_date(int dateNum) {
    Date date;
    date.y = dateNum/10000;
    date.m = dateNum/100%100;
    date.d = dateNum%100;
    return date;
}

// 下一天
Date next_day(Date date) {
    date.d += 1;
    if(date.d > get_md(date.y, date.m)) {
        date.m += 1;
        date.d = 1;
    }
    if(date.m > 12) {
        date.y += 1;
        date.m = 1;
        date.d = 1;
    }
    return date;
}

// 判断两个日期是否相等
bool equal(Date date1, Date date2) {
    return date1.y == date2.y && date1.m == date2.m && date1.d == date2.d;
}

int main()
{
    int dateNum1, dateNum2;
    while(cin>>dateNum1>>dateNum2) {
        int count = 1;
        Date date1 = get_date(dateNum1);
        Date date2 = get_date(dateNum2);
        while(!equal(date1, date2)) {
            count++;
            date1 = next_day(date1);
        }
        cout<<count<<endl;
    }
    return 0;
}

发表于 2018-03-24 15:04:53 回复(0)
写的有点复杂,其实思路很简单,但是中间的各种判断相加啥的写的有点复杂。。。献丑了
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
int i,j,sum;
int m0[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int m1[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int a[8];

int test(int year)//判断闰年
{
    if(year%400==0)
        return 1;
    else
    {
        if(year%4==0 && year%100!=0)
            return 1;
        else
            return 0;
    }
}

void get_info(int &date,int &year,int &mon,int &day)//从输入分别获取年月日
{
    memset(a,0,sizeof(a));
    i=10000000;
    j=0;
    while(date>0)
    {
        a[j]=date/i;
        if(a[j]>=0)
        {
            date-=a[j]*i;
            j++;
        }
        i/=10;
    }
    year=a[0]*1000+a[1]*100+a[2]*10+a[3];
    mon=a[4]*10+a[5];
    day=a[6]*10+a[7];
}

void count_day(int year1,int mon1,int day1,int year2,int mon2,int day2)//当输入不是同一年时获取结果
{
    sum=1;
    for(i=year1+1;i<year2;i++)
    {
        if(test(i)==1)
            sum+=366;
        else
            sum+=365;
    }
    if(test(year1)==1)
    {
        for(i=mon1+1;i<=12;i++)
            sum+=m1[i-1];
        sum=sum+m1[mon1-1]-day1;
    }
    else{
        for(i=mon1+1;i<=12;i++)
            sum+=m0[i-1];
        sum=sum+m0[mon1-1]-day1;
    }

    if(test(year2)==1)
    {
        for(i=1;i<mon2;i++)
            sum+=m1[i-1];
        sum=sum+day2;
    }
    else{
        for(i=1;i<mon2;i++)
            sum+=m0[i-1];
        sum=sum+day2;
    }
    cout<<sum<<endl;
}
void count_day_same(int year,int mon1,int day1,int mon2,int day2)//当输入是同一年时获取结果
{
    if(test(year)==1)
    {
        if(mon1<mon2)
        {
            for(i=mon1+1;i<mon2;i++)
                sum+=m1[i-1];
            sum+=m1[mon1-1]-day1+1;
            sum+=day2;
        }
        else if(mon1>mon2)
        {
            for(i=mon2+1;i<mon1;i++)
                sum+=m1[i-1];
            sum+=m1[mon2-1]-day2+1;
            sum+=day1;
        }
        else
            sum=day1>day2?(day1-day2+1):(day2-day1+1);
    }
    else{
        if(mon1<mon2)
        {
            for(i=mon1+1;i<mon2;i++)
                sum+=m0[i-1];
            sum+=m0[mon1-1]-day1+1;
            sum+=day2;
        }
        else if(mon1>mon2)
        {
            for(i=mon2+1;i<mon1;i++)
                sum+=m0[i-1];
            sum+=m0[mon2-1]-day2+1;
            sum+=day1;
        }
        else
            sum=day1>day2?(day1-day2+1):(day2-day1+1);
    }
    cout<<sum<<endl;
}

int main(void)
{
    int date1,date2;
    int year1,year2,mon1,mon2,day1,day2;
    while(cin>>date1>>date2)
    {
        get_info(date1,year1,mon1,day1);
        get_info(date2,year2,mon2,day2);
        int s=day1>day2?(day1-day2):(day2-day1);
        if(year1==year2&&mon1==mon2&&s==1)
        {
            cout<<"2"<<endl;
            break;
        }
        else if(year1<year2)
            count_day(year1,mon1,day1,year2,mon2,day2);
        else if(year1>year2)
            count_day(year2,mon2,day2,year1,mon1,day1);
        else
            count_day_same(year1,mon1,day1,mon2,day2);
    }
    return 0;
}
发表于 2019-11-24 15:04:07 回复(0)

思路:

将小的日期记为A,大的日期记为B

计算 A年的 1.1 (A11) 到B年1.1 (B11)的天数 A11toB11 (记得计算闰年)

计算 A到本年的日期 Ato11

计算 B到本年的日期 Bto11

日期差值为

A11toB11 + Bto11 - Ato11

代码如下

#include
#include
#include 
using namespace std; 
int beginDayOfMonth[13][2] = {
    0, 0,
    0, 0,
    31, 31,
    59, 60,
    90, 91,
    120, 121,
    151, 152,
    181, 182,
    212, 213,
    243, 244,
    273, 274,
    304, 305,
    334, 335 
};
int isRunYear(int year)
{
    return year%100 != 0 && year%4 == 0 || year%400 == 0 ? 1:0;
} 
int dayto11(int year, int month, int day)
{
    int isRun = isRunYear(year);
    if(isRun)
    {
         return day + beginDayOfMonth[month][1];
    }
    else return day + beginDayOfMonth[month][0];
    // return day + beginDayOfMonth[month][isRun];  但这里需要保证 isRun 只取 01 
} 
int main()
{
    int AtoA11, BtoB11, A11toB11;
    int y1, m1, d1;
    int y2, m2, d2;
    int disDay;
    while(scanf("%4d%2d%2d", &y1, &m1, &d1) != EOF)
    {
        scanf("%4d%2d%2d", &y2, &m2, &d2);
        if(y1 > y2 || (y1 == y2 && m1 > m2) || (y1 == y2 && m1 == m2 && d1 > d2))
        {
            int tmp = y1;
            y1 = y2;
            y2 = tmp;
            tmp = m1;
            m1 = m2;
            m2 = tmp;
            tmp = d1;
            d1 = d2;
            d2 = tmp;    
        }
        AtoA11 = dayto11(y1, m1, d1);
        BtoB11 = dayto11(y2, m2, d2);
        // cout << AtoA11 << endl;
        // cout << BtoB11 << endl;
        A11toB11 = 0;
        for(int i = y1; i < y2; i++)
        {
            if(isRunYear(i)) A11toB11 += 366;
            else A11toB11 += 365;
        }
        disDay = A11toB11 + BtoB11 - AtoA11 + 1;  // 这里是 把 最后一天的也给算上 
        printf("%d\n", disDay);     
    }
    return 0;
}

思路二
主要参考“看不见的风景201803281904”。
这里我对计算距离0年1月1日的方法进行了修改,

#include<cstdio>
#include<iostream> 
#include<cmath> 
using namespace std;
int mon[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

int sumRun1(int y)
{
    // 计算[0, y]之间 闰年 的次数 
    // 若 y在 0000 年之前, 则为负数,记录了[y, -1]闰年的次数(的相反数) 
    return 1 + floor(double(y-1)/4+1e-4) - floor(double(y-1)/100+1e-4)  + floor(double(y-1)/400+1e-4);
    // y-1是因为如果 第y年是闰年,那么会对 00000101至 y+1年1月1日 的天数造成影响 
    // +1 是为了 将0000年记为闰年 
    // floor(double(y-1)/4+1e-4) 是为了 计算在公元前的闰年 
    // + 1e-4 是为了避免浮点误差

    /* 
    其实这段代码的效果就是为了对应
    年数 
        -5 -4 -3 -2 -1  0  1  2  3  4  5  6  
    累计闰年个数
        -1 -1  0  0  0  0  1  1  1  1  2  2 

    */ 
} 
int isRunYear(int year)
{
    return year%100 != 0 && year%4 == 0 || year%400 == 0 ? 1:0;
} 
int sumRun2(int y) // 和这个效果是一样的
{
    int numOfRun = 0;
    if(y < 0)
    {
        for(int i = y; i < 0; i++)
        {
            if(isRunYear(i)) numOfRun--;
        }    
    }
    else
    {
        for(int i = 0; i < y; i++)
        {
            if(isRunYear(i)) numOfRun++;
        }    
    } 

    return numOfRun;
}
// 给出年月日,计算距离0000年 1 月 1 日的 天数 
int calto01(int y, int m, int d)
{
    return sumRun1(y) + y * 365 + mon[m-1] + d-1 + (((y%100 != 0 && y%4 == 0) || y%400 == 0) && m > 2); 
}

int main()
{
    int disto01[2], year, month, day;
    int y0, m0, d0;
    int y1, m1, d1;
    while(scanf("%4d%2d%2d", &y0, &m0, &d0) != EOF)
    {
        scanf("%4d%2d%2d", &y1, &m1, &d1);
        disto01[0] = calto01(y0, m0, d0);
        disto01[1] = calto01(y1, m1, d1);
        // cout << disto01[0] << " " << disto01[1] << endl; 
        printf("%d\n", abs(disto01[0] - disto01[1]) + 1);
    }
    return 0;
}
编辑于 2019-05-14 17:14:45 回复(0)
献丑了,用了最笨的方法了,一天一天数过去的。。。
思路:从较小的日期开始,一天一天往上增加,直到等于较大日期为止。
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

int bijiao(int y1,int m1,int d1,int y2,int m2,int d2){  //比较日期大小
    if(y1>y2)
        return 1;
    else if((y1==y2)&&(m1>m2))
        return 1;
    else if((y1==y2)&&(m1==m2)&&(d1>d2))
        return 1;
    return 0;
}

int runnian(int a){ //判断是否是闰年
    if((a%400==0)||(a%100!=0&&a%4==0))
        return 1;
    return 0;
}

int main(){
    long int a,b;
    cin>>a>>b;
    int s1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int s2[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    int y1=a/10000;
    int y2=b/10000;
    int m1=a/100%100;
    int m2=b/100%100;
    int d1=a%100;
    int d2=b%100;
    int k=0;
    if(bijiao(y1,m1,d1,y2,m2,d2)==0){   //总是让y1对应的时间是最晚的
        swap(y1,y2);
        swap(m1,m2);
        swap(d1,d2);
    }
    while(y1!=y2||m1!=m2||d1!=d2){  //一天一天往上增加,直到等于最晚的时间
        if(runnian(y2)){    //若是闰年则使用数组s2,否则s1
            if((d2+1)>s2[m2-1]){    //d2增加一天,判断是否超出月份
                if(m2+1>12){    //超出月份则月份加一,判断是否过了12月
                    y2++;   //超过12月则年份加一,时间归1
                    m2=1;
                    d2=1;
                }
                else {  //若没有超过年份,则月份加1,天数变为1;
                    m2++;
                    d2=1;
                }
            }
        else {  //若是没有超过月份,则天数加一
            d2++;
        }
    }
    else {  //不是闰年,进行相同操作
        if((d2+1)>s1[m2-1]){
                if(m2+1>12){
                    y2++;
                    m2=1;
                    d2=1;
                }
                else {
                    m2++;
                    d2=1;
                }
            }
        else {
            d2++;
        }
    }
    k++;//每次天数加一后,K++
    }
    cout<<++k; //最后输出要增加一天
}

编辑于 2019-03-12 20:53:19 回复(0)

分别对年月日进行差值计算累加天数

#coding : utf-8

year = [365, 366]
month = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]]
def isRun(y):
    if y%400==0 or (y%100!=0 and y%4==0):
        return 1
    return 0
def count(y1, m1, d1, y2, m2, d2):
    cnt = 0
    for y in xrange(y1, y2):
        cnt+=year[isRun(y)]
    y2_isRun = isRun(y2)
    for m in xrange(m1, m2):
        cnt+=month[y2_isRun][m-1]
    return cnt+d2-d1+1
while True:
    try:
        date1 = raw_input().strip()
        date2 = raw_input().strip()
        y1 = int(date1[0:4])
        y2 = int(date2[0:4])
        m1 = int(date1[4:6])
        m2 = int(date2[4:6])
        d1 = int(date1[6:8])
        d2 = int(date2[6:8])
        if(y1>y2) or (y1==y2 and m1>m2) or (y1==y2 and m1==m2 and d1>d2):
            y1 = int(date2[0:4])
            y2 = int(date1[0:4])
            m1 = int(date2[4:6])
            m2 = int(date1[4:6])
            d1 = int(date2[6:8])
            d2 = int(date1[6:8])
        print count(y1, m1, d1, y2, m2, d2)
    except:
        break
发表于 2019-01-07 11:24:37 回复(0)
细节题!!
#include <stdio.h>
#include <math.h>
char date1[10],date2[10];
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
struct DATE{
    int y;
    int m;
    int d;
} dt1,dt2,tmp;
int is_LeapYear(int year){
    return ((year%4==0 && year%100!=0) || (year%400==0))?1:0;
}
int main(){
    int i,res;
    while(scanf("%s",date1)!=EOF){
        scanf("%s",date2);
        dt1.y=dt1.m=dt1.d=0;
        dt2.y=dt2.m=dt2.d=0;
        for(i=0;i<4;i++){
            dt1.y+=((date1[i]-'0')*pow(10,3-i));
            dt2.y+=((date2[i]-'0')*pow(10,3-i));
        }
        for(i=4;i<6;i++){
            dt1.m+=((date1[i]-'0')*pow(10,5-i));
            dt2.m+=((date2[i]-'0')*pow(10,5-i));
        }
        for(i=6;i<8;i++){
            dt1.d+=((date1[i]-'0')*pow(10,7-i));
            dt2.d+=((date2[i]-'0')*pow(10,7-i));
        }
        if((dt1.y>dt2.y) || (dt1.y==dt2.y && dt1.m>dt2.m)
            || (dt1.y==dt2.y && dt1.m==dt2.m && dt1.d>dt2.d)){
            tmp=dt1;
            dt1=dt2;
            dt2=tmp;
        }
        //年月相同
        if(dt1.y==dt2.y && dt1.m==dt2.m)
            res=dt2.d-dt1.d+1;
        //年相同
        else if(dt1.y==dt2.y){
            res=0;
            if(is_LeapYear(dt1.y))
                month[2]=29;
            else
                month[2]=28;
            for(i=dt1.m+1;i<dt2.m;i++)
                res+=month[i];
            res+=(month[dt1.m]-dt1.d+1);
            res+=(dt2.d);
        }
        //年不同
        else{
            res=0;
            for(i=dt1.y+1;i<dt2.y;i++){
                if(is_LeapYear(i))
                    res+=366;
                else
                    res+=365;
            }
            if(is_LeapYear(dt1.y))
                month[2]=29;
            else
                month[2]=28;
            for(i=dt1.m+1;i<=12;i++)
                res+=month[i];
            res+=(month[dt1.m]-dt1.d+1);
            if(is_LeapYear(dt2.y))
                month[2]=29;
            else
                month[2]=28;
            for(i=1;i<dt2.m;i++)
                res+=month[i];
            res+=(dt2.d);
        }
        printf("%d\n",res);
    }
    return 0;
}

发表于 2019-01-04 09:39:04 回复(0)
import datetime
while True:
    try:
        date1 = input()
        date2 = input()
        print((datetime.datetime(int(date2[:4]),int(date2[4:6]),int(date2[6:]))-datetime.datetime(int(date1[:4]),int(date1[4:6]),int(date1[6:]))).days+1)
    except Exception:
        break
编辑于 2018-10-13 14:18:07 回复(0)
import java.io.BufferedInputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;

public class Main
{
    public static void main(String... as)
    {
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        LocalDate ld1 = LocalDate.parse(sc.next(), DateTimeFormatter.ofPattern("yyyyMMdd"));
        LocalDate ld2 = LocalDate.parse(sc.next(), DateTimeFormatter.ofPattern("yyyyMMdd"));
        System.out.println(ld1.until(ld2, ChronoUnit.DAYS) + 1);
        sc.close();
    }
    
}

发表于 2018-09-19 16:04:49 回复(0)
//因为还不会格式输入,所以输入年月日的时候用了笨办法,希望大佬能帮忙改一下,谢谢!
#include<iostream>
#define ISYEAP(x) ((x%4==0&&x%100!=0)||x%400==0) ?1:0

using namespace std;
int dayofmonth[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>dayofmonth[month][ISYEAP(year)])
        {
            day = 1;
            month++;
            if (month>12)
            {
                month = 1;
                year++;
            }//if
        }//if
    }//nextday
};//data

int buf[5001][12][32];

int abs(int x) {
    return x<0 ? -x : x;
}
int main() {
    Date tmp;
    tmp.year = 0;
    tmp.month = 1;
    tmp.day = 1;
    int num = 0;
    while (tmp.year < 5001) {
        buf[tmp.year][tmp.month][tmp.day] = num;
        tmp.nextday();
        num++;
    }//while
    int d1, m1, y1, temp1;
    int d2, m2, y2, temp2;
    while (cin >> temp1) {
        d1 = temp1 % 100; temp1 = temp1 / 100;
        m1 = temp1 % 100; y1 = temp1 / 100;
        cin >> temp2;
        d2 = temp2 % 100; temp2 = temp2 / 100;
        m2 = temp2 % 100; y2 = temp2 / 100;
        cout << abs(buf[y2][m2][d2] - buf[y1][m1][d1])+1<< endl;
    }
    return 0;
}

发表于 2018-02-26 04:03:58 回复(0)

献丑了。

#include<cstdio>
struct Time {
    int y;
    int m;
    int d;
}t1,t2;
bool Is366(int year){
    return (year % 4 == 0 && year % 100 != 0 || year % 100 == 0 && year % 400 == 0);
}
int month(int year, int month){
    int Is31[7] = { 1,3,5,7,8,10,12 };
    for (int i = 0; i < 7; i++){
        if (Is31[i] == month)
            return 31;
    }
    if (month == 2){
        if (Is366(year))
            return 29;
        else return 28;
    }
    else return 30;
}
void swap(struct Time *a, struct Time *b) {
    struct Time temp;
    temp=*a;
    *a = *b;
    *b = temp;
}
int main(){    
    int val;
    while (scanf("%4d%2d%2d %4d%2d%2d", &t1.y, &t1.m, &t1.d, &t2.y, &t2.m, &t2.d) != EOF) {
        if (t1.y > t2.y) swap(&t1, &t2);
        else if(t1.y==t2.y){
            if (t1.m > t2.m) swap(&t1, &t2);
            else if(t1.m==t2.m)
                if(t1.d>t2.d) swap(&t1, &t2);
        }
        if (t1.y == t2.y) {
            if (t1.m == t2.m)
                val = t2.d - t1.d + 1;
            else {
                val = month(t1.y, t1.m) - t1.d + 1 + t2.d;
                for (int i = t1.m + 1; i <= t2.m - 1; i++)
                    val += month(t1.y, i);
            }
        }
        else {
            val = month(t1.y, t1.m) - t1.d + 1;
            for (int i = t1.m + 1; i <= 12; i++)
                val += month(t1.y, i);
            for (int i = t1.y + 1; i < t2.y; i++) {
                if (Is366(i)) val += 366;
                else val += 365;
            }
            for (int i = 1; i < t2.m; i++)
                val += month(t2.y, i);
            val += t2.d;
        }
        printf("%d\n", val);
    }
    return 0;
}
编辑于 2018-01-06 15:54:02 回复(0)
#include<stdio.h>
#define isyeap(x) x%100!=0&&x%4==0||x%400==0?1:0
int dayOfMonth[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>dayOfMonth[month][isyeap(year)])
    {
        day=1;
        month++;
        if(month>12)
        {
            month=1;
            year++;
        }
    }
    }
};
int buf[5001][13][32];
int abs(int x)
{
    return x<0?-x:x;
}
int main()
{
    Date tmp;
    int cnt=0;
    tmp.day=1;
    tmp.month=1;
    tmp.year=0;
    while(tmp.year!=5001)
    {
        buf[tmp.year][tmp.month][tmp.day]=cnt;
        tmp.nextDay();
        cnt++;
    }
    int d1,m1,y1;
    int d2,m2,y2;
    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);
    }
}
 
发表于 2018-01-03 18:54:30 回复(0)
题目本身不难,设定好一个基准日期1年1月1日做差即可。使用一个类或者结构体表示日期能方便一些,不知道c++有没有类似python的直接时间类。
#include <iostream>
#include <sstream>
using namespace std;
class Date
{
public:
    int year;
    int month;
    int day;
};
int Judge(int year)
{
    if((year%4==0&&year%100!=0)||year%400==0)
        return 1;
    else return 0;
}
int s2i(string s1)
{
    double a ;
    stringstream ss;
    ss << s1;
    ss >> a;
    return a;
}
int Days(Date day)
{
    int sum = 0;
    for(int i = 1;i<day.month;i++)
    {
        if(i==1||i==3||i==5||i==7||i==8||i==10) sum = sum+31;
        else if(i==4||i==6||i==9||i==11) sum = sum+30;
        else if(i==2) sum = sum+28+Judge(day.year);
    }
    sum = sum+day.day;
    return sum;
}
int calculate(Date d1,Date d2)
{
    int sum1=0,sum2=0;
    for(int i = 1;i<d1.year;i++)
        sum1 = sum1+365+Judge(i);
    for(int k = 1;k<d2.year;k++)
        sum2 = sum2+365+Judge(k);
    sum1 = sum1+Days(d1);
    sum2 = sum2+Days(d2);
    return sum2-sum1+1;
}
int main()
{
    string date1,date2;
    while(cin>>date1>>date2)
    {
        Date d1,d2;
        d1.year = s2i(date1.substr(0,4));d1.month = s2i(date1.substr(4,2));d1.day = s2i(date1.substr(6,2));
        d2.year = s2i(date2.substr(0,4));d2.month = s2i(date2.substr(4,2));d2.day = s2i(date2.substr(6,2));
        cout<<calculate(d1, d2)<<endl;
    }  
}

发表于 2019-10-20 15:33:40 回复(0)
#include<iostream>
#include<algorithm>
using namespace std;

#define isyeap(x) (x % 100 !=0 && x % 4 == 0 ) || x % 400 == 0  ? 1 : 0

struct date {
	int year;
	int month;
	int day;
};

int daysofmonth[2][13] = { { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };

int count(date a) {
	int mycount = 0;
	for (int i = 0; i < a.year; i++) {
		if (isyeap(i)) mycount += 366;
		else mycount += 365;
	}
	for (int i = 0; i < a.month; i++) {
		if (isyeap(a.year)) {
			mycount += daysofmonth[0][i];
		}
		else mycount += daysofmonth[1][i];
	}
	mycount += a.day;
	return mycount;
}

int main() {
	date a, b;
	while (scanf("%4d%2d%2d", &a.year, &a.month, &a.day) != EOF) {
		scanf("%4d%2d%2d", &b.year, &b.month, &b.day);
		int num1 = count(a);
		int num2 = count(b);
		cout << abs(num1 - num2) + 1 << endl;
	}
	return 0;
}

发表于 2019-09-04 19:47:07 回复(0)
while True:
    try:
        inp1=list(input().strip())
        inp2=list(input().strip())
        list1=[31,28,31,30,31,30,31,31,30,31,30,31]
        list2=[31,29,31,30,31,30,31,31,30,31,30,31]
        min_num=min(int(''.join(inp1)),int(''.join(inp2)))
        max_num=max(int(''.join(inp1)),int(''.join(inp2)))
        def isrunnian(i):
            if i%100!=0 and i%4==0:
                return True
            elif i%400==0:
                return True
            else:
                return False
        def data(i,info):
            result=0
            month=int(i[4:6])
            day=int(i[6:])
            if info:
                result+=sum(list2[:month-1])
                result+=day
            else:
                result+=sum(list1[:month-1])
                result+=day
            return result
        if isrunnian(int(str(min_num)[0:4])):
            info1=True
        else:
            info1=False
        if isrunnian(int(str(max_num)[0:4])):
            info2=True
        else:
            info2=False
        sum1=data(str(min_num),info1)
        sum2=data(str(max_num),info2)
        min_year=int((str(min_num)[0:4]))
        max_year=int((str(max_num)[0:4]))
        for i in range(min_year,max_year):
            if isrunnian(i):
                sum2+=366
            else:
                sum2+=365
        print(sum2-sum1+1)

    except:
        break
发表于 2019-08-20 10:00:57 回复(0)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define IFYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0

int yearDay[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
};

typedef struct date {
    int year;
    int month;
    int day;
    void nextDate() {    //进入下一天
        day++;
        if (day > yearDay[month][IFYEAP(year)]) {
            month++;
            day = 1;
            if (month > 12) {
                month = 1;
                year++;
            }
        }
    }
}Date;

int main() {
    //计算从0000年1月1日到5000年12月31日每一天距离0000年1月1日的天数,并存储在buf中

    Date date1, date2;
    int cnf = 0;
    scanf("%4d%2d%2d", &date1.year, &date1.month, &date1.day);
    scanf("%4d%2d%2d", &date2.year, &date2.month, &date2.day);
    int index = -1;
    if (date1.year > date2.year) index = 2;
    else if (date1.year < date2.year) index = 1;
    else {
        if (date1.month > date2.month) index = 2;
        else if (date1.month < date2.month) index = 1;
        else {
            cnf = abs(date1.day - date2.day) + 1;
        }
    }
    switch (index) {
    case -1: break;
    case 1:
        while (date1.year != date2.year || date1.month != date2.month || date1.day != date2.day) {
            date1.nextDate();
            cnf++;
        }
        break;
    case 2:
        while (date1.year != date2.year || date1.month != date2.month || date1.day != date2.day) {
            date2.nextDate();
            cnf++;
        }
        break;
    }
    printf("%d\n", cnf+1);
    return 0;
}
发表于 2019-07-30 16:52:03 回复(0)
#include <iostream>
using namespace std;
bool isLeapYear(int year)
{
 if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
 {
  return true;
 }
 return false;
}
class date{
 int year;
 int month;
 int day;
public:
 date(long int dateA)
 {
  year = dateA / 10000;
  day = dateA % 100;
  month = dateA % 10000 / 100;
 }
 date()
 {
  year = 0;
  month = 0;
  day = 0;
 }
 date& increase()
 {
  int Month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  if(isLeapYear(year))
  {
   Month[2] = 29;
  }
  else Month[2] = 28;
  if(month < 12)
  {
   if(day != Month[month])
   {
    day++;
    return *this;
   }
   else{
    day = 1;
    month ++;
    return *this;
   }
  }
  else
  {
   if(day != Month[month])
   {
    day++;
    return *this;
   }
   else{
    day = 1;
    month = 1;
    year++;
    return *this;
   }
  }
 }
 
 date operator =(const date& dateA)
 {
  date temp;
  temp.day = dateA.day;
  temp.month = dateA.month;
  temp.year = dateA.year;
  return temp;
 }
 
 bool operator <(const date& dateA)
 {
  if(year < dateA.year)
   return true;
  else if(year > dateA.year)
   return false;
  else{
   if(month < dateA.month)
    return true;
   else if(month > dateA.month)
    return false;
   else{
    if(day < dateA.day)
    {
     return true;
    }
    return false;
   }
  }
 }
 
 int operator -(const date& dateA)
 {
  int diff = 0;
  date temp = dateA;
  for(diff = 1; temp < *this ; diff++)
  {
   temp = temp.increase();
  }
  return diff;
 }
};

//----------------------------------------main---------------------------------------------------
int main()
{
 long int i,j;
 while(cin >> i >> j)
 {
  date A(j), B(i);
  cout << A - B << endl;
 }
 return 0;
}
发表于 2019-05-05 21:22:41 回复(0)
#include<bits/stdc++.h>
using namespace std;
//判是否为闰年
int is_leap(int n)
{
    if((n%4==0&&n%100!=0)||n%400==0)
        return 1;
    return 0;
}
int main()
{
    //每个月份的天数
    int month[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}};
    long a,b;
    while(cin>>a>>b)
    {
        int year1,year2,mon1,mon2,day1,day2;
        int sum = 0;
        int mark;
        //分离出年月日
        year1 = a/10000;
        year2 = b/10000;
        mon1 = a%10000/100;
        mon2 = b%10000/100;
        day1 = a%10000%100;
        day2 = b%10000%100;
        
        if(year1!=year2)
        {
            mark = is_leap(year1);
            sum  += month[mark][mon1]-day1+1;
            for(int i= mon1+1;i<=12;i++)
                sum+=month[mark][i];
            year1++;
            while(year1!=year2)
            {
                if(is_leap(year1))
                    sum+=366;
                else sum+= 365;
                year1++;
            }
            mark = is_leap(year2);
            for(int i=1;i<mon2;i++)
            {
                sum+=month[mark][i];
            }
            sum+= day2;
        }
        else{
            if(mon1!=mon2)
            {
                 mark = is_leap(year2);
                 sum += month[mark][mon1]-day1+1;
                
                for(int i=mon1+1;i<mon2;i++)
               {
                   sum+=month[mark][i];
               }
                sum+= day2;
                 
            }
            else{
                sum = day2 - day1 + 1;
            }
        }
           
        cout<<sum<<endl;
        
    }
    return 0;
}

发表于 2019-04-16 16:40:43 回复(0)