Calendar

题目:

R - Calendar
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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.

Input

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).

Output

Print a single integer — the answer to the problem.

Sample Input

Input
1900:01:01
2038:12:31
Output
50768
Input
1996:03:09
1991:11:12
Output
1579
 
    
 
    
代码:
#include <stdio.h>

int isLeapYear(int y);
int dayToNow(int month);
void swap(int *a, int *b);

int main()
{
	int sy, sm, sd, ey, em, ed, i, leapYear, commonYear, ans,
		sdd, edd, syfeday, eyfeday;

	while(~scanf("%d:%d:%d%d:%d:%d", &sy, &sm, &sd, &ey, &em, &ed))
	{
		if((sy > ey) || ((sy == ey) && (sm > em)) || ((sy == ey) && (sm == em) && (sd > ed))) 
		{ swap(&sy, &ey), swap(&sm, &em), swap(&sd, &ed); }

		//[sy.01.01 , ey.01.01]
		leapYear = 0;
		syfeday = eyfeday = 28;
		for(i = sy; i < ey; i++)
		{
			if(isLeapYear(i)) leapYear++;
		}
		if(isLeapYear(sy))  syfeday = 29;
		if(isLeapYear(ey))  eyfeday = 29;
		commonYear = ey - sy - leapYear;

		//[sy.01.01 ,sy.sm.sd)
		sdd = dayToNow(sm);
		sdd += sd - 1;
		if(sm > 2) sdd += syfeday;

		//[ey.01.01 , ey.em.sd)
		edd = dayToNow(em);
		edd += ed - 1;
		if(em > 2) edd += eyfeday;

		ans = leapYear * 366 + commonYear * 365 - sdd + edd ;
		printf("%d\n", ans);
	}

	return 0;
}

void swap(int *a, int *b)
{
	int tmp;
	tmp = *a;
	*a = *b;
	*b = tmp;
}

int isLeapYear(int y)
{
	if((0 == y % 400) || ((0 == y % 4) && (0 != y%100)))
		return 1;
	return 0;
}

int dayToNow(int month)
{
	int day;
	switch(month)
	{
	case 1:  day = 0;   break;
	case 2:  day = 31;  break;
	case 3:  day = 31;  break;
	case 4:  day = 62;  break;
	case 5:  day = 92;  break;
	case 6:  day = 123; break;
	case 7:  day = 153; break;
	case 8:  day = 184; break;
	case 9:  day = 215; break;
	case 10: day = 245; break;
	case 11: day = 276; break;
	case 12: day = 306; break;
	}
	return day;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值