Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1619
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1619
Date Difference
By Henrique Pinto, UFMG Brazil
Timelimit: 1
Joanna is working on the new version of her company's blog. One of the changes she wants to make is how post dates are displayed. In the current version, dates are displayed in the year-month-day format (e.g. 2014-08-09). She wishes that dates would instead be displayed as the number of days elapsed between the current date and the post date. So, for example, if today is August 9, 2014, then the date 2014-05-23 would be displayed as "78 days ago", and the date 2014-08-07 would be "2 days ago".
Joanna is very busy with some of the more complex problems related to the new blog, and asked you for help with this task. Given two dates, compute the number of days elapsed between them.
Notes
Please remember that leap years have a 366th day (February 29). A year is a leap year if its number is a multiple of 400, or if it is a multiple of 4 but not a multiple of 100.
Input
Input begins with a line containing a single integer N, the number of test cases (0 < N <= 10000). After that, there are N lines, each of which describes a test case.
Each of those lines contains two dates, separated by a single space. Dates are in the YYYY-MM-DD format, where YYYY is the year, MM in the month, and DD is the day. You can assume that all dates in the input are valid (i.e., there won't be any dates like 2013-02-31 in the input). All dates are between 1970 and 2014 (including). The month and day are always given with two digits, with a leading zero if necessary. For example, February 3, 2014 would be in the input as 2014-02-03.
Output
For each test case, print a line containing the absolute value of the number of days elapsed between the two dates.
Sample Input | Sample Output |
4 2014-07-27 2014-07-25 2014-08-09 2014-05-23 2012-01-01 2013-01-01 1970-01-01 1970-01-01 |
2 78 366 0 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
bool leapyear(int year)
{
if(year%400==0)return true;
if(year%100!=0&&year%4==0)return true;
return false;
}
int a[13];
int b[13];
void sieve()
{
a[1]=0;
for(int i = 2; i <= 12; i++)
{
if(i==2||i==4||i==6||i==9||i==11)
a[i]=a[i-1]+31;
if(i==3)a[i]=a[i-1]+28;
if(i==5||i==7||i==10||i==12)
{
a[i]=a[i-1]+30;
}
if(i==8)a[i]=a[i-1]+31;
}
b[1]=0;
for(int i = 2; i < = 12; i++)
{
if(i==2||i==4||i==6||i==9||i==11)
b[i]=b[i-1]+31;
if(i==3)b[i]=b[i-1]+29;
if(i==5||i==7||i==10||i==12)
{
b[i]=b[i-1]+30;
}
if(i==8)b[i]=b[i-1]+31;
}
//for(int i = 1; i < = 12; i++)
//cout<<a[i]<<endl;
}
int yearsum(int y)
{
int sum=0;
for(int i =1971; i < = y; i++)
{
if(leapyear(i-1))sum+=366;
else sum+=365;
}
return sum;
}
int main()
{
sieve();
int t;
cin>>t;
while(t--)
{
int y1,y2;
int m1,m2;
int d1,d2;
scanf("%d-%d-%d",&y1,&m1,&d1);
scanf("%d-%d-%d",&y2,&m2,&d2);
int day1,day2;
day1=yearsum(y1);
day2=yearsum(y2);
int tr=a[m1]+d1;
if(leapyear(y1))
{
tr=b[m1]+d1;
}
day1+=tr;
tr=a[m2]+d2;
if(leapyear(y2))
{
tr=b[m2]+d2;
}
day2+=tr;
cout << abs(day1-day2> << endl;
}
}
Copy The Code &
Try With Live Editor
Input
2014-07-27 2014-07-25
2014-08-09 2014-05-23
2012-01-01 2013-01-01
1970-01-01 1970-01-01
Output
78
366
0