21世纪对称日
21世纪对称日有20011002、20100102、20111102、20200202、20211202、20300302、20400402、20500502、20600602、20700702、20800802、20900902。
2021年12月2日,因公历纪年日期中数字“20211202”左右完全对称,而被人们称为“完全对称日”。在这个罕见又特殊的日子里,不少新人为了讨个好彩头,告白、结婚;也有一些集邮爱好者专程去邮局盖日戳,留下一份纪念。
package main
import "fmt"
var (
yearStart = 2000
yearEnd = 2099
monthArray = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
commonDayArray = []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
leapDayArray = []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
)
func main() {
isLeap := func(y int) bool {
return (y%4 == 0 && y%100 != 0) || y%400 == 0
}
toArray := func(i int) []int {
res := []int{}
res = append(res, i/10000000%10)
res = append(res, i/1000000%10)
res = append(res, i/100000%10)
res = append(res, i/10000%10)
res = append(res, i/1000%10)
res = append(res, i/100%10)
res = append(res, i/10%10)
res = append(res, i/1%10)
return res
}
isSym := func(date int) bool {
numArray := toArray(date)
return numArray[0] == numArray[7] &&
numArray[1] == numArray[6] &&
numArray[2] == numArray[5] &&
numArray[3] == numArray[4]
}
getSymArray := func() []int {
symDate := []int{}
for year := yearStart; year <= yearEnd; year++ {
dayArray := commonDayArray
if isLeap(year) {
dayArray = leapDayArray
}
for index, month := range monthArray {
monthMaxDay := dayArray[index]
for day := 1; day <= monthMaxDay; day++ {
date := year*10000 + month*100 + day
if isSym(date) {
symDate = append(symDate, date)
}
}
}
}
return symDate
}
print := func(arr []int) {
msg := "公元" + fmt.Sprint(yearStart) + "年~" + fmt.Sprint(yearEnd) + "年之间的完全对称日如下:"
fmt.Println(msg)
for _, date := range arr {
fmt.Println(date)
}
}
print(getSymArray())
}
结果如下: