Loading... 这个题目需要求时针和分针成180度的次数和对应时间点,我们知道时针每个小时走30度,每分钟还会走0.5度,分针每分钟走6度,所以要求的夹角就是abs(时针走的角度-分针走的角度)=180即可。 那么abs(30 * h + 0.5 * m - 6 * m) = 180 -> 30 * h - 5.5 * m = ±180 -> m = (30 * h ± 180) / 5.5 所以只需要遍历h=0-11,由上式得到的 m 在 0-60 之间则有效。 ```go package main import ( "fmt" "math" ) func main() { count := 0 for h := 0; h < 24; h++ { for _, sign := range []int{-1, 1} { m := (30*float64(h%12) + 180*float64(sign)) / 5.5 if m < 0 || m >= 60 { continue } mi := int(m) sec := int(math.Round((m - float64(mi)) * 60)) if sec == 60 { sec = 0 mi++ } count++ fmt.Printf("%02d:%02d:%02d\n", h, mi, sec) } } fmt.Println("24小时出现次数:", count) } ``` Last modification:July 22, 2025 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 如果觉得我的文章对你有用,请随意赞赏