Calendar 练习

来源:互联网 发布:拳皇开发源码 编辑:程序博客网 时间:2024/05/20 12:51

题目描述

 Calendars in widespread use today include the Gregorian calendar, which is the de facto international standard, and is used almost everywhere in the world for civil purposes. The Gregorian reform modified the Julian calendar's scheme of leap years as follows:

Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 is not a leap year; the year 2000 is a leap year.
                                      
In this problem, you have been given two dates and your task is to calculate how many days are between them. Note, that leap years have unusual number of days in February.

Look at the sample to understand what borders are included in the aswer.

输入

 The first two lines contain two dates, each date is in the format yyyy:mm:dd (1900 ≤ yyyy ≤ 2038 and yyyy:mm:dd is a legal date).

输出

 Print a single integer — the answer to the problem.

示例输入

1900:01:012038:12:31

示例输出

50768
注释:注意题目未明确说明输入的先后顺序,只是在给定的合理日期内,输出他们之间间隔的天数。
#include <stdio.h>int main(){int y1,y2,m1,m2,d1,d2,sum,flag,num,temp,year;scanf("%d:%d:%d",&y1,&m1,&d1);scanf("%d:%d:%d",&y2,&m2,&d2);if(y1>y2 || (y1==y2 && m1>m2) || (y1==y2 && m1==m2 && d1>d2) ){temp=y1;y1=y2;y2=temp;temp=m1;m1=m2;m2=temp;temp=d1;d1=d2;d2=temp;}for(year=y1,num=0;year<y2;year++)if((year%4==0 && year%100!=0) || year%400==0)num++;sum=(y2-y1)*365+num;flag=((y1%4==0 && y1%100!=0) || y1%400==0);sum-=((m1>1)+(m1>3)+(m1>5)+(m1>7)+(m1>8)+(m1>10))*31+((m1>4)+(m1>6)+(m1>9)+(m1>11))*30+(m1>2)*(28+flag)+d1;flag=((y2%4==0 && y2%100!=0) || y2%400==0);sum+=((m2>1)+(m2>3)+(m2>5)+(m2>7)+(m2>8)+(m2>10))*31+((m2>4)+(m2>6)+(m2>9)+(m2>11))*30+(m2>2)*(28+flag)+d2;printf("%d\n",sum);return 0;}


原创粉丝点击