九度OJ 1096 日期差值,九度oj1096差值

來源:互聯網
上載者:User

九度OJ 1096 日期差值,九度oj1096差值

題目1096:日期差值

時間限制:1 秒

記憶體限制:32 兆

特殊判題:否

提交:5143

解決:1789

題目描述:

有兩個日期,求兩個日期之間的天數,如果兩個日期是連續的我們規定他們之間的天數為兩天

輸入:

有多組資料,每組資料有兩行,分別表示兩個日期,形式為YYYYMMDD

輸出:

每組資料輸出一行,即日期差值

範例輸入:
2011041220110422
範例輸出:
11


#include<stdio.h>#include<string.h>#include<stdlib.h>int months[]={0,31,28,31,30,31,30,31,31,30,31,30,31};int isLeapYear(int y){    if((y%4==0&&y%100!=0)||y%400==0)return 1;    return 0;}int getdays(int y,int m,int d,int flag){    int result=0;    if(isLeapYear(y))    {        months[2]+=1;        int i=1;        while(i<m){            result+=months[i];            i++;        }        result+=d;        months[2]-=1;        if(flag==0)        {            return 366-result+1;        }        else            return result;    }    int i=1;    while(i<m){        result+=months[i];        i++;    }    result+=d;    if(flag==0)    {        return 365-result+1;    }    else        return result;} int main(int argc, char *argv[]){    int y1,m1,d1,y2,m2,d2;    char s1[10];    char s2[10];    char t[10];    while(~scanf("%s%s",s1,s2))    {        strncpy(t,s1,4);        t[4]='\0';        y1=atoi(t);        strncpy(t,s1+4,2);        t[2]='\0';        m1=atoi(t);        strncpy(t,s1+6,2);        t[2]='\0';        d1=atoi(t);         strncpy(t,s2,4);        t[4]='\0';        y2=atoi(t);        strncpy(t,s2+4,2);        t[2]='\0';        m2=atoi(t);        strncpy(t,s2+6,2);        t[2]='\0';        d2=atoi(t);        //printf("%d %d %d ---- %d %d %d\n",y1,m1,d1,y2,m2,d2);        if(y1!=y2)        {            int re=getdays(y1 , m1 , d1 , 0);            for(int i=y1+1;i<y2;++i)            {                if(isLeapYear(i))                    re+=366;                else                    re+=365;            }            re+=getdays(y2, m2 , d2 ,1);            printf("%d\n",re);        }        else            printf("%d\n",getdays(y2 , m2 , d2 , 1)-getdays(y1 , m1 , d1 , 1)+1);    }    return 0;} /**************************************************************    Problem: 1096    User: kirchhoff    Language: C    Result: Accepted    Time:0 ms    Memory:920 kb****************************************************************/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.