The Same Calendar
Title
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.
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.
模拟:先定义一个函数来判断输入的年份是不是闰年,如果是闰年就返回2,平年返回1(2和1分别为闰年和平年对7取模所得的天数),最后判断只有当输入年份和最后所得年份同为闰年或同为平年并且这两年中间的差为整周数时符合题意。
#include<cstdio>
#include<iostream>
int special(int a);
using namespace std;
int main(){
int k,sum=0;
scanf("%d",&k);
int k1=k;
while(1){
k1++;
sum+=special(k1);
if(sum%7==0&&special(k)==special(k1))break;
}
cout<<k1;
return 0;
}
int special(int a){
if(a%400==0)return 2;
if(a%100==0)return 1;
if(a%4==0)return 2;
return 1;
}