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