Calendar CodeForces - 304B(两个日期的差)

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.

Examples
Input
1900:01:01
2038:12:31
Output
50768
Input
1996:03:09
1991:11:12
Output
1579

日期的差,本来c++课上写过这个。当时在网上找了找(因为自己写的一百多行),而这个只需要三行代码就可以。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int dis_sum(int d,int m,int y)//!!!!
{
	int month=(m+9)%12;//用于判断日期是否大于3月(2月是判断闰年的标识),还用于纪录到3月的间隔月数。
	int year=y-month/10;//如果是1月和2月,则不包括当前年(因为是计算到0年3月1日的天数)。
	return 365*year+year/4- year/100+year/400+(month*306 + 5)/10+(d-1);//365*y 是不算闰年多出那一天的天数,y/4 - y/100 + y/400 是加所有闰年多出的那一天,(m*306 + 5)/10 用于计算到当前月到3月1日间的天数,306=365-31-28(1月和2月),5是全年中不是31天月份的个数。(day - 1) 用于计算当前日到1日的间隔天数,
}

char s[100];

int main()
{
	gets(s);
	int a1=(s[0]-'0')*1000+(s[1]-'0')*100+(s[2]-'0')*10+(s[3]-'0');
	int b1=(s[5]-'0')*10+s[6]-'0';
	int c1=(s[8]-'0')*10+s[9]-'0';
	gets(s);
	int a2=(s[0]-'0')*1000+(s[1]-'0')*100+(s[2]-'0')*10+(s[3]-'0');
	int b2=(s[5]-'0')*10+s[6]-'0';
	int c2=(s[8]-'0')*10+s[9]-'0';
	printf("%d\n",abs(-dis_sum(c1,b1,a1)+dis_sum(c2,b2,a2)));
}

努力加油a啊,(o)/~

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/84580564
今日推荐