题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
package main import ( "fmt" "sort" ) func calculateSum(nums [][2]int) { m := make(map[int]int, 0) for _, pair := range nums { m[pair[0]] += pair[1] } var keyList []int for key, _ := range m { keyList = append(keyList, key) } sort.Ints(keyList) for _, key := range keyList { fmt.Printf("%d %d\n", key, m[key]) } } func main() { var N int var nums [][2]int fmt.Scan(&N) for i:=0; i<N; i++ { var a, b int fmt.Scan(&a, &b) nums = append(nums, [2]int{a, b}) } calculateSum(nums) }
// 本题输入为数字,所以采用 fmt.Scan(&a, &b) 的方式接受输入