CodeForces - 678B - B. The Same Calendar

£神魔★判官ぃ 2022-06-12 13:49 307阅读 0赞

题目连接: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

  1. 2016

Sample Output

  1. 2044

Sample Input

  1. 2000

Sample Output

  1. 2048

Sample Input

  1. 50501

Sample Output

  1. 50507

解题思路

计算两个相同的一年,一样的话,如果1月1号是周1,那么输的那一年也是周1,那么中间的天数得是7 的倍数,365 % 7 = 1, 366 % 7 = 2,如果是平年就加 1 ,闰年加 2 ,直到是7的倍数停止 ,注意,如果开始是平年的话,结果也是平年

AC代码

  1. #include<iostream>
  2. using namespace std;
  3. int check(int n) {
  4. if((n%400 == 0) || (n%4 == 0 && n%100 != 0)) return 1;
  5. return 0;
  6. }
  7. int main () {
  8. int y;
  9. while(~scanf("%d", &y)) {
  10. int sum = 0;
  11. int i;
  12. for(i = y+1; ; i++) {
  13. if(check(i)) {
  14. sum += 2;
  15. }
  16. else {
  17. sum += 1;
  18. }
  19. if(sum%7 == 0 && (check(y) == check(i)))
  20. break;
  21. }
  22. printf("%d\n", i);
  23. }
  24. return 0;
  25. }

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读