題目1096:日期差值 (2009年上海交通大學電腦研究生機試真題)

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   sp   strong   資料   div   

題目描述:

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

輸入:

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

輸出:

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

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


代碼如下:
#include "stdio.h"  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}; typedef struct Date{    int year;    int month;    int day;}Date; int isRunNian(int year){  //判斷是否是閏年    if((year%100!=0&&year%4==0)||(year%400==0))        return 1;    else        return 0;} int compare(Date d1,Date d2){    if(d1.year>d2.year)        return 1;    else        if(d1.year<d2.year)            return 0;        else{            if(d1.month>d2.month)                return 1;            else                if(d1.month<d2.month)                    return 0;                else{                    if(d1.day>d2.day)                        return 1;                    else                        if(d1.day<d2.day)                            return 0;                        else                            return 2;//相同日期                }        }     } void nextDay(Date* date){  //計算下一天    (*date).day++;    if((*date).day>dayOfMonth[(*date).month][isRunNian((*date).year)]){        (*date).day=1;        (*date).month++;        if((*date).month>12){            (*date).month=1;            (*date).year++;        }    }} int main(int argc, char* argv[]){    Date date1,date2;    while(scanf("%4d%2d%2d",&date1.year,&date1.month,&date1.day)!=EOF){        scanf("%4d%2d%2d",&date2.year,&date2.month,&date2.day);        //printf("%d\n",compare(date1,date2));        //printf("date1.year:%d  date1.month:%d  date1.day:%d\n",date1.year,date1.month,date1.day);        //printf("date2.year:%d  date2.month:%d  date2.day:%d\n",date2.year,date2.month,date2.day);        Date big_Date,small_Date;        if(compare(date1,date2)==1){            big_Date.year=date1.year;                   big_Date.month=date1.month;            big_Date.day=date1.day;            small_Date.year=date2.year;                 small_Date.month=date2.month;            small_Date.day=date2.day;        }        else{            big_Date.year=date2.year;                   big_Date.month=date2.month;            big_Date.day=date2.day;            small_Date.year=date1.year;                 small_Date.month=date1.month;            small_Date.day=date1.day;        }        int count=1;//用於計數        while(compare(small_Date,big_Date)==0||compare(small_Date,big_Date)==1){//小日期追趕大日期,count計數            count++;            nextDay(&small_Date);        }        printf("%d\n",count);    }    return 0;} /**************************************************************    Problem: 1096    User: lcyvino    Language: C    Result: Accepted    Time:10 ms    Memory:912 kb****************************************************************/

 

題目1096:日期差值 (2009年上海交通大學電腦研究生機試真題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.