lightoj 1414

来源:互联网 发布:入门java看什么书 编辑:程序博客网 时间:2024/06/03 20:07

lightoj 1414

题目:February 29

It is 2012, and it’s a leap year. So there is a “February 29” in this year, which is called leap day. Interesting thing is the infant who will born in this February 29, will get his/her birthday again in 2016, which is another leap year. So February 29 only exists in leap years. Does leap year comes in every 4 years? Years that are divisible by 4 are leap years, but years that are divisible by 100 are not leap years, unless they are divisible by 400 in which case they are leap years.

In this problem, you will be given two different date. You have to find the number of leap days in between them.

解题思路:

  • 怎么判断一个年份是闰年:(year % 4 == 0 && year % 100 != 0) || (year % 400)。
  • 在计算年份之间有几个闰年是怎样避免计算重复:容斥定理, year / 4 - year / 100 + year / 400
  • 怎么处理两个端点:当端点年份为闰年时,小的年份如果小于润天, 结果+1,大的年份如果小于润天,结果-1.