The Same Calendar
The girl Taylor has a beautiful calendar for the year y. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
The calendar is so beautiful that she wants to know what is the next year after ywhen the calendar will be exactly the same. Help Taylor to find that year.
Note that leap years has 366 days. The year is leap if it is divisible by 400 or it is divisible by 4, but not by 100 (https://en.wikipedia.org/wiki/Leap_year).
Input
The only line contains integer y (1000 ≤ y < 100'000) — the year of the calendar.
Output
Print the only integer y' — the next year after y when the calendar will be the same. Note that you should find the first year after y with the same calendar.
Examples
input:2016
output:2044
input:2000
output:2028
input:50501
output:50507
#include<stdio.h>
#include<math.h>
int countweek(int year,int month,int day)
{
int y,c,week;
y=year%100;
c=year/100;
if (month ==1 || month == 2)
{
month += 12;
y--;
}
week=y+y/4+c/4-2*c+26*(month+1)/10+day-1;
if(year%400==0&&month>12)
week--;
while(week<0)
{
week+=7;
}
week=week%7;
return week;
}
int main()
{
int year;
while(scanf("%d",&year)!=EOF)
{
int a=countweek(year,2,2),b=countweek(year,3,1);
while(1)
{
year+=1;
if(countweek(year,2,2)==a&&countweek(year,3,1)==b)
break;
}
printf("%d\n",year);
}
}