题解 | #合并表记录#

合并表记录

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)
        }
    }
}

全部评论

相关推荐

牛至超人:把哈工大,再加大加粗,看见闪闪发光的哈工大字样,面试官直接流口水
投递字节跳动等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务