Go 语言实现:判断是否可以将所有数字加起来等于目标值
package main\nimport ('bufio';'fmt';'os';'strconv';'strings')\nfunc main() {\n\treader := bufio.NewReader(os.Stdin)\n\tl, _, _ := reader.ReadLine()\n\tt, _ := strconv.Atoi(string(l))\n\n\tfor i := 0; i < t; i++ {\n\t\tl, _, _ := reader.ReadLine()\n\t\tnumsStr := strings.Split(string(l), '\ ')\n\t\tnums := []int{}\n\t\tfor _, numStr := range numsStr {\n\t\t\tnum, _ := strconv.Atoi(numStr)\n\t\t\tnums = append(nums, num)\n\t\t}\n\t\tn, s := nums[0], nums[1]\n\t\tfor j := 0; j < n; j++ {\n\t\t\tl, _, _ := reader.ReadLine()\n\t\t\tnum, _ := strconv.Atoi(string(l))\n\t\t\ts -= num\n\t\t}\n\t\tif s == 0 {\n\t\t\tfmt.Println('YES')\n\t\t} else {\n\t\t\tfmt.Println('NO')\n\t\t}\n\t}\n}
原文地址: https://www.cveoy.top/t/topic/pCAW 著作权归作者所有。请勿转载和采集!