Algorithm
Problem Name: 2 AD-HOC - beecrowd | 2315
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2315
Easy Difference Between Dates
By Leonardo Fernandes, IFSC Brazil
Timelimit: 1
Your program must read two dates (day and month) and calculate the difference between them, in days. Consider it's a regular year (february has 28 days).
Input
The input has two rows with two integers each, representing day and month of the first and second dates. The first date is always smaller than or equal to the second.
Output
The output is the number of days between the first and second date.
Input Samples | Output Samples |
21 2 1 3 |
8 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#define true 1
#define false 0
int main (void)
{
unsigned short i;
unsigned short ans;
unsigned short a, b, c, d;
scanf("%hu %hu", &a, &b);
scanf("%hu %hu", &c, &d);
ans = 0;
if (b == d)
ans = c - a;
else
{
for (i = b; i < = d; ++i)
{
if (i == 4 || i == 6 || i == 9 || i == 11)
{
if (i == b)
ans += 30 - a;
else if (i == d)
ans += c;
else
ans += 30;
}
else if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
{
if (i == b)
ans += 31 - a;
else if (i == d)
ans += c;
else
ans += 31;
}
else
{
if (i == b)
ans += 28 - a;
else if (i == d)
ans += c;
else
ans += 28;
}
}
}
printf("%hu\n", ans);
}
Copy The Code &
Try With Live Editor
Input
1 3
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
int c,d;
cin >> a >> b;
cin >> c >> d;
int day=0;
//day=a;
if(b==d)
{
day=c-a;
}
else{
for(int i = b;i <= d; i++)
{
if(i==4||i==6||i==9||i==11)
{
if(i==b)
{
day+=30-a;
}
else if(i==d)
{
day=day+c;
}
else
day=day+30;
}
else if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{
if(i==b)
{
day+=31-a;
}
else if(i==d)
{
day=day+c;
}
else
day=day+31;
}
else
{
if(i==b)
{
day+=28-a;
}
else if(i==d>
{
day=day+c;
}
else
day=day+28;
}
}
}
cout << day << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
1 3
Output