package com.patience.interview.huawei;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Map;
import java.util.TreeMap;
/**
* 合并表记录
* @author Green.Gee
* @date 2022/7/27 14:35
* @email green.gee.lu@gmail.com
*/
public class MergeTb {
public static void main(String[] args) throws Exception {
Map<Integer,Integer> map = new TreeMap<>();
StreamTokenizer in = new StreamTokenizer(
new BufferedReader(
new InputStreamReader(System.in)));
in.nextToken();
int n = (int)in.nval;
int a, b;
while(in.nextToken() != StreamTokenizer.TT_EOF){
a = (int)in.nval;
in.nextToken();
b = (int)in.nval;
Integer k = map.get(a);
if(k == null){
map.put(a,b);
}else{
map.put(a,b + k);
}
n--;
if(n == 0)
break;
}
map.forEach((k,v)->{
System.out.println(k+" "+v);
});
}
}
合并表记录,排序,值求和
最新推荐文章于 2024-08-15 14:47:41 发布