You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lavos/cap3/work21.go

42 lines
1.1 KiB

7 months ago
package cap3
import "math"
func Work21(callback func(num, sqrt int)) {
// 检查两个数是否由9个不同的数字组成
checkDigitUnique := func(a, b int) bool {
// 创建一个map来跟踪每个数字是否已经出现过
mark := map[int]bool{}
// 检查第一个数的每一位数字
for ; a > 0; a /= 10 {
// 如果数字已经在map中,直接返回false
if _, exists := mark[a%10]; exists {
return false
} else {
// map标记
mark[a%10] = true
}
}
for ; b > 0; b /= 10 {
if _, exists := mark[b%10]; exists {
return false
} else {
mark[b%10] = true
}
}
// map中的数字总数为9,返回true
return len(mark) == 9
}
for num1 := 100000; num1 < 1000000; num1++ {
for num2 := 100; num2 < 1000; num2++ {
if math.Sqrt(float64(num1)) == float64(num2) && checkDigitUnique(num1, num2) {
callback(num1, num2)
}
}
}
}