习题2.6-日期差值

#include<iostream>
using namespace std;

int num[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 year[2]={365,366};

int main(){
    int y1,y2,m1,m2,d1,d2;
    while(scanf("%4d%2d%2d%4d%2d%2d",&y1,&m1,&d1,&y2,&m2,&d2)!=EOF){
        int days=1;
        int row1=0,row2=0;
        if(y1%400==0||(y1%100!=0&&y1%4==0)) row1=1;
        while(m1>0){
            d1+=num[row1][--m1];
        }
        if(y2%400==0||(y2%100!=0&&y2%4==0)) row2=1;
        while(m2>0){
            d2+=num[row2][--m2];
        }
        if(y1==y2) days+=d2-d1;
        else{
            days+=year[row1]-d1+d2;
            while(++y1<y2){
                row1=0;
                if(y1%400==0||(y1%100!=0&&y1%4==0)) row1=1;
                days+=year[row1];
            }
        }
        printf("%d\n",days);
    }
    return 0;
}

 

发布了69 篇原创文章 · 获赞 5 · 访问量 6337
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览