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 y when 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
Note
Today is Monday, the 13th of June, 2016.
这道题首先想的是两个年份中间夹着的日期数目可以被7整除,其次还要想到是否同闰年或者都不是闰年,不然2月份后日历还是不一样(我一开始也没想到这,还觉得自己没想错,太菜了,菜鸟人)
#include<iostream>
using namespace std;
int main()
{
int y,ytoo;
long long sum=0;
int flag=0;
cin>>y;
if(y%400==0||(y%4==0&&y%100!=0))
{
flag=1;
}
for(int i=y+1;i>=0;i++)
{
if(i%400==0||(i%4==0&&i%100!=0))
{
sum=sum+366;
}
else
{
sum=sum+365;
}
if(sum%7==0)
{
if(i%400==0||(i%4==0&&i%100!=0))
{
if(flag==1)
{
ytoo=i;
break;
}
else
{
continue;
}
}
else
{
if(flag==0)
{
ytoo=i;
break;
}
else
{
continue;
}
}
}
}
cout<<ytoo<<endl;
return 0;
}