题解 | #合并表记录#

合并表记录

https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

import java.util.*; 

public class Main {
    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            int N = Integer.parseInt(in.nextLine());      
            Map<Integer,Integer> res = new HashMap<>();
            int[] sum = new int[N];
            for (int i=0;i<N;i++){
                String str = in.nextLine();
                String[] strings = str.split(" ");
                sum[i] = Integer.parseInt(strings[0]);
                if (res.get(sum[i])==null) res.put(sum[i],Integer.parseInt(strings[1]));
                else res.put(sum[i],Integer.parseInt(strings[1])+res.get(sum[i]));
            }
            for (int i=0;i<N-1;i++){
                for(int j = i+1;j<N;j++){
                    if (sum[i]>sum[j]) {
                        int ll = sum[i];
                        sum[i] = sum[j];
                        sum[j] = ll;
                    }
                }
            }
            for (int i=0;i<N;i++){
                if (i==0||sum[i]!=sum[i-1]) System.out.println(sum[i]+" "+res.get(sum
                [i]));
            }
        }
    }
}

全部评论

相关推荐

07-04 16:00
门头沟学院 Java
点赞 评论 收藏
分享
zhiyog:哈哈哈哈哈哈哈哈哈哈哈哈哈
点赞 评论 收藏
分享
05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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