关卡地址
解决方案:
思路:
解释下这个日历,顶部是月份、年份,中间是当月日历,底部分别是上个月与下个月的缩略图
在源代码有两条提示:
he ain’t the youngest, he is the second
todo: buy flowers for tomorrow
1月26日日程:为明天买花。说明1**6年1月27日是一个值得纪念的日子。
从图中右下角可以看到,2月份有29号,说明那一年是闰年。
根据以上信息,年份需满足以下条件:
- 闰年
- 年份最后一位是6
- 该年1月1日是星期四
- 所有满足条件的倒数第二个年份
注:如果起始年份为闰年(如1016、1996),则年份步进可选为10与4的最小公倍数20。否则年份步进应为10。
最后得到的年份是1756年。
1756年1月27日,奥地利音乐大师莫扎特诞生。
代码:
import calendar
leapYears=[]
for year in range(1016, 1996+1, 20):
# 是闰年并且该年1月1日是星期四
if calendar.isleap(year) and calendar.weekday(year,1,1)==3:
leapYears.append(year)
print(leapYears[-2])
PS src\static> python .\Code\Python\Challenge015.py
$LeapYears=@()
for ($year = 1016; $year -le 1996; $year+=20) {
# 是闰年并且该年1月1日是星期四
if ([datetime]::IsLeapYear($year) -and [datetime]::new($year,1,1).DayOfWeek -eq [System.DayOfWeek]::Thursday) {
$LeapYears+=$year
}
}
$LeapYears[-2]
PS src\static> .\Code\PowerShell\Challenge015.ps1
package main
import(
"fmt"
"time"
)
func (c *Challenge) Challenge015() {
var leapYears []int
for year := 1016; year <= 1996; year+=20 {
// 是闰年并且该年1月1日是星期四
if isLeapYear(year) {
date := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
if date.Weekday()==4 {
leapYears=append(leapYears, year)
}
}
}
fmt.Println(leapYears[len(leapYears)-2])
}
func isLeapYear(year int) bool {
return year%100==0 && year%400==0 || year%4==0
}
PS src\static> .\Code\Go\Challenge.exe -l 015