青春纸盒子

文: 芦苇

你喜欢我笑的样子

我靠上了落寞的窗子

晚风吹起了我的袖子

明月沾湿了你的眸子


转身,你走出了两个人的圈子

树影婆娑,整座院子


挽起袖子

回头,把揽你忧伤一地的影子

装进,青春,这纸盒子


更多代码请关注我的微信小程序: "ecoder"

luwei0915

导航

Go题库8_合并表记录

package main

import (
    "bufio"
    "fmt"
    "os"
    "sort"
    "strconv"
    "strings"
)

func main() {
    scanner := bufio.NewScanner(os.Stdin)

    for scanner.Scan() {
        mp := make(map[int]int, 500)
        input := scanner.Text()
        if len(input) == 0 {
            break
        }

        end, _ := strconv.Atoi(input) // str转int
        for i := 0; i < end; i++ {
            scanner.Scan()                        // 重新扫描
            arr := strings.Fields(scanner.Text()) // 字符串分隔
            k, _ := strconv.Atoi(arr[0])
            v, _ := strconv.Atoi(arr[1])

            if val, ok := mp[k]; ok { // 判断 map 键值对是否存在
                mp[k] = v + val
            } else {
                mp[k] = v
            }
        }

        keys := make([]int, 0, 500) // 提取 keys
        for key := range mp {
            keys = append(keys, key)
        }
        sort.Ints(keys) // 数组排序
        for _, item := range keys {
            fmt.Printf("%d %d\n", item, mp[item])
        }
    }

}

 

posted on 2021-11-05 11:58  芦苇の  阅读(11)  评论(0编辑  收藏  举报