CodeForces - 678B - B. The Same Calendar
题目连接:http://codeforces.com/problemset/problem/678/B
题目描述
Description
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.
Sample Input
2016
Sample Output
2044
Sample Input
2000
Sample Output
2048
Sample Input
50501
Sample Output
50507
解题思路
计算两个相同的一年,一样的话,如果1月1号是周1,那么输的那一年也是周1,那么中间的天数得是7 的倍数,365 % 7 = 1, 366 % 7 = 2,如果是平年就加 1 ,闰年加 2 ,直到是7的倍数停止 ,注意,如果开始是平年的话,结果也是平年
AC代码
#include<iostream>
using namespace std;
int check(int n) {
if((n%400 == 0) || (n%4 == 0 && n%100 != 0)) return 1;
return 0;
}
int main () {
int y;
while(~scanf("%d", &y)) {
int sum = 0;
int i;
for(i = y+1; ; i++) {
if(check(i)) {
sum += 2;
}
else {
sum += 1;
}
if(sum%7 == 0 && (check(y) == check(i)))
break;
}
printf("%d\n", i);
}
return 0;
}
还没有评论,来说两句吧...