MHGOC03 - Editorial

Question

Count Fridays the 13th

Difficulty level

Easy

Prerequisites

None

Code

#include
using namespace std;
int month[12]={3,3,0,3,2,3,2,3,3,2,3,2};
int d1,d2,m1,m2,s1,s2,a=0;

int isleap(int year)
{
if (year%4 != 0 || (year%100 == 0 && year%400 != 0))
return 0;
else
return 1;
}

long calcd(int year,int mon,int day)
{
int d=2,y,i;
long fri=0;
for(y=1900;y<=year;y++)
{
if(isleap(y)==1)
month[2]=1;
else
month[2]=0;
for(i=1;i<=12;i++)
{
d = (d+month[i-1])%7;
if(d==4)
fri++;
if(y==year&&i==mon&&day<13&&d==4)
{
fri–;
break;
}
else if(y==year&&i==mon&&d1==13&&d==4)
{
a=1;
break;
}
else if(y==year&&i==mon)
break;
}
}

return fri;
}
int main() {

int t;
long fri;
cin>>t;
while(t–)
{
cin>>d1>>m1>>s1;
cin>>d2>>m2>>s2;
fri = calcd(s2,m2,d2)-calcd(s1,m1,d1)+a;
a=0;
cout<< fri <<"\n";
}
return 0;
}