题解 | #称砝码#
称砝码
https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c
package main import ( "fmt" ) func main() { var n int fmt.Scan(&n) weights := make([]int, n) for i := 0; i < n; i++ { var w int fmt.Scan(&w) weights[i] = w } all := []int{} // 所有元素装到一个切片里 for i := 0; i < n; i++ { var num int fmt.Scan(&num) for j:=0;j<num;j++{ all = append(all, weights[i]) } } set := map[int]struct{}{0:struct{}{}} // 用map当作set用 for _,v := range all{ temp := []int{} for k,_ := range set { // map是引用类型,要先深度拷贝,不然后面的循环会循环到新增元素 temp = append(temp,k) } for _,k := range temp{ set[k+v] = struct{}{} } } fmt.Println(len(set)) }
// 本题输入为三行数字,所以采用: fmt.Scan(&n)