Files
codekata-golang/19_counting_sundays/main.go
2020-10-19 16:09:01 +08:00

35 lines
833 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import "fmt"
func main() {
sum := 0
for y := 1901; y <= 2000; y++ {
for m := 1; m <= 12; m++ {
if calWeek(y, m, 1) == 0 {
sum++
}
}
}
fmt.Println("sum:", sum)
}
func calWeek(y, m, d int) int {
//w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
//
//公式中的符号含义如下w星期c世纪y两位数
// mm大于等于3小于等于14即在蔡勒公式中某年的1、2月要看作上一年的13、14月来计算
// 比如2003年1月1日要看作2002年的13月1日来计算d[ ]代表取整,即只要整数部分。
if m == 1 || m == 2 {
y--
m += 12
}
c := y / 100
y = y - c*100
return (y + y/4 + c/4 - 2*c + 26*(m+1)/10 + d - 1) % 7
}
func isLeapYear(year int) bool {
return year%400 == 0 || (year%4 == 0 && year%100 != 0)
}