Calendar 练习

题目描述

 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:01
2038: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;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值