题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
var rows int
fmt.Scan(&rows)
if rows <= 0 {
return
}
// 防止越界
result := make([]int, 11111112)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
arr := strings.Split(line, " ")
index, _ := strconv.Atoi(arr[0])
value, _ := strconv.Atoi(arr[1])
// 同索引相加
result[index] += value
}
for index, value := range result {
// 正整数
if value > 0 {
fmt.Println(index, value)
}
}
}
查看8道真题和解析
