CodeForces - 304B - Calendar (思维题)

CodeForces - 304B - 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.

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

题目大意:
就是求两个时间之间差了多少天

解题思路:
考虑闰年,2月29还是28天,还有每个月有多少天。
正好前几天C++课上写了这么一个类。。。我就拿来用一下,判断一下两个日期谁前谁后,然后日期小的那个开始一天天的加,一直加到和日期大的那个一样。思路很简单,就是细节处理的地方比较多,看一下代码把。(因为是拿了之前写的类,可能有些东西是不必要的,看官选择性看)

AC代码

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
class Cdate{
public:
	int year;
	int mouth;
	int day;
	const int mouthday[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};	
public:

	Cdate(int a,int b,int c)
	{
		year=a;
		mouth=b;
		day=c;
	}
	~Cdate()
	{
//		cout<<"析构成功"<<endl;
	}
	void print();
	bool judge();//判断闰年
	void add_oneday();
	void lose_oneday();
	void add(int x);
	void lose(int x);
};
bool Cdate::judge()
{
	if(year%100==0)
	{
		if(year%400==0)
			return true;
		else
			return false;
	}
	else
	{
		if(year%4==0)
			return true;
		else
			return false;
	}
}
void Cdate::add_oneday()
{
	if(day<mouthday[mouth])
	{
		day++;
	}	
	else 
	{
		if(judge()&&mouth==2)///闰年
		{
			
			if(day==29)
			{
				mouth++;
				day=1;
			}
			else
				day++;//29天
			return;
		} 
		if(mouth<12)
		{
			day=1;
			mouth++;
		}
		else if(mouth==12)
		{
			year++;
			mouth=1;
			day=1;
		}
	}
}

int judge1(Cdate &a , Cdate &b)
{
	if(a.year>b.year)
		return 1;
	else if(a.year<b.year)
		return 0;
	else if(a.year==b.year)
	{
		if(a.mouth>b.mouth)
			return 1;
		else if(a.mouth<b.mouth)
			return 0;
		else 
		{
			if(a.day<b.day)
				return 1;
			else if(a.day<b.day)
				return 0;
			else
				return 2;
		}
	}
}

int main()
{

	int year,mouth,day;
	scanf("%d:%d:%d",&year,&mouth,&day);
	Cdate date1(year,mouth,day);
	scanf("%d:%d:%d",&year,&mouth,&day);
	Cdate date2(year,mouth,day);
//	cout<<date1.year<<" "<<date1.mouth<<" "<<date1.day<<endl;
//	cout<<date2.year<<" "<<date2.mouth<<" "<<date2.day<<endl;

	int ans=0;
	if(judge1 (date1,date2)==2)
		ans=0;
	else if(judge1(date1,date2)==1)
	{
	
		while(1)
		{
			ans++;
			//cout<<ans<<endl;
			date2.add_oneday();
			if(date2.year==date1.year&&date1.mouth==date2.mouth&&date2.day==date1.day)
				break;
		}
	}
	else //2 大
	{
		while(1)
		{
		
			date1.add_oneday();
			ans++;
			//cout<<ans<<endl;
			if(date2.year==date1.year&&date1.mouth==date2.mouth&&date2.day==date1.day)
				break;
		}
	}
	cout<<ans<<endl;

	
	return 0;

}


//2010:3:2  2010:4:30
//2019:4:60 2020:1:2
//2010:2:28 2011:2:28
//2018:1:1 2019:1:2
//2018:1:1 2020:1:1 

猜你喜欢

转载自blog.csdn.net/weixin_43179892/article/details/84576977
今日推荐