题解 | 魔法棒
魔法棒
https://www.nowcoder.com/practice/976bd95dda8f4430b512d0c39bd9f106
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
// 使用 map 存储需要判断的数字,查询效率为 O(1)
noNumbers := map[int64]bool{
2: true,
3: true,
5: true,
6: true,
8: true,
11: true,
14: true,
}
scanner := bufio.NewScanner(os.Stdin)
// 读取测试用例个数 t
scanner.Scan()
tStr := scanner.Text()
t, err := strconv.Atoi(tStr)
if err != nil {
fmt.Println("读取测试用例个数失败:", err)
return
}
// 循环处理每个测试用例
for i := 0; i < t; i++ {
scanner.Scan()
nStr := strings.TrimSpace(scanner.Text())
n, err := strconv.ParseInt(nStr, 10, 64)
if err != nil {
fmt.Println("读取数字失败:", err)
return
}
// 判断 n 是否在 noNumbers 集合中
if noNumbers[n] {
fmt.Println("No")
} else {
fmt.Println("Yes")
}
}
// 检查 scanner 是否有错误
if err := scanner.Err(); err != nil {
fmt.Println("读取输入失败:", err)
}
}
查看16道真题和解析
