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.
|
|
|
package cap3
|
|
|
|
|
|
|
|
func Work18(callback func(four, five 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 four := 1000; four <= 9999; four++ {
|
|
|
|
// 计算当前四位数的两倍
|
|
|
|
five := 2 * four
|
|
|
|
|
|
|
|
// 检查结果是否为五位数
|
|
|
|
if 10000 <= five && five <= 99999 {
|
|
|
|
// 调用checkUnique函数检查数字是否互不相同,条件满足则回调
|
|
|
|
if checkDigitUnique(four, five) {
|
|
|
|
callback(four, five)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|