第一行输入一个整数
代表数据表的记录数。
此后
行,第
行输入两个整数
代表数据表的第
条记录的索引和数值。
一共若干行(视输入数据变化),第
行输出两个整数,代表合并后数据表中第
条记录的索引和数值。
4 0 1 0 2 1 2 3 4
0 3 1 2 3 4
在这个样例中,第
条记录索引相同,合并数值为
。
2 0 1 0 1
0 2
不能用LinkedHashMap(那是按插入顺序进行排序的),而是用TreeMap(对键进行排序)
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i = 0; i < n; i++){
int key = in.nextInt();
int value = in.nextInt();
if (!map.containsKey(key)){
map.put(key, value);
}else{
map.merge(key, value, Integer :: sum);
}
}
for (Integer key : map.keySet()){
System.out.println(key + " " + map.get(key));
}
}
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n = sc.nextInt();//输入行数
if (n < 1 || n > 500) {
System.out.println("请输入正确的条数");
return;
}
//创建一个Map存放后续输入的数字,大小为输入的n
Map<Integer, Integer> map = new HashMap<>(n);
int key;
int value;
for (int i = 0; i < n; i++) {
//循环输入数字表
key = sc.nextInt();
value = sc.nextInt();
if (map.containsKey(key)) {//判断输入的key在map中是否存在,
//如果存在,则将value相加后存入map中
map.put(key, value + map.get(key));
} else {
//不存在,则直接插入map中
map.put(key, value);
}
}
//创建set,来存放map的key值
Set<Integer> set = map.keySet();
//创建list,将set中的数字存入其中
Integer[] list = new Integer[set.size()];
set.toArray(list);
//对list进行排序操作
Arrays.sort(list);
for (Integer i :
list) {//排序后顺序遍历list中的数字,并输出map中对应key的value
System.out.println(i + " " + map.get(i));
}
sc.close();
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n= in.nextInt();
TreeMap<Integer, Integer> mergedMap=new TreeMap<>();
for(int i=0;i<n;++i){
int key=in.nextInt();
int value=in.nextInt();
mergedMap.put(key,mergedMap.getOrDefault(key,0)+value);
}
for(Map.Entry<Integer,Integer> entry: mergedMap.entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
} import java.util.LinkedList;
import java.util.Scanner;
import java.util.TreeMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
TreeMap<Integer,Integer> treeMap = new TreeMap<>();
for (int i = 0; i < n; i++) {
int key = in.nextInt();
int value = in.nextInt();
if(treeMap.containsKey(key)){
value+=treeMap.get(key);
}
treeMap.put(key,value);
}
for (Integer key:treeMap.keySet()) {
System.out.println(key+" "+treeMap.get(key));
}
}
} import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<Integer, Integer> inMap = new HashMap<>();
int allLine = in.nextInt();
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int key = in.nextInt();
int value = in.nextInt();
if (inMap.containsKey(key)){
inMap.replace(key, inMap.get(key) + value);
}
else{
inMap.put(key, value);
}
}
Set set=inMap.keySet();
Object[] keyArr=set.toArray();
Arrays.sort(keyArr);
for(Object key : keyArr){
System.out.println(key + " " + inMap.get(key));
}
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
int index = scanner.nextInt();
int value = scanner.nextInt();
map.put(index, map.getOrDefault(index, 0) + value);
}
// 将map转换为树状结构,以便按照索引排序
List<Map.Entry<Integer, Integer>> sortedEntries = new ArrayList<>
(map.entrySet());
sortedEntries.sort(Map.Entry.comparingByKey());
for (Map.Entry<Integer, Integer> entry : sortedEntries) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
scanner.close();
}
} import java.util.Scanner;
import java.util.TreeMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int key;
int value;
TreeMap<Integer,Integer> TreeMap = new TreeMap<>();
for (int i = 1; i <= count; i++ ) {
key=in.nextInt();
value=in.nextInt();
if (TreeMap.get(key)==null) {
TreeMap.put(key,value);
} else {
value=TreeMap.get(key)+value;
TreeMap.put(key,value);
}
}
for (int getKey : TreeMap.keySet()) {
System.out.println(getKey + " " + TreeMap.get(getKey));
}
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<Integer, Integer> map = new TreeMap<>();
int rows = scanner.nextInt();
for (int i = 0; i < rows; i++) {
int index = scanner.nextInt();
int value = scanner.nextInt();
if (map.containsKey(index)) {
map.put(index, map.get(index) + value);
}else {
map.put(index, value);
}
}
for (Integer index : map.keySet()) {
System.out.println(index + " " + map.get(index));
}
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int[][] array = null;
int linecnt = 1;
int cnt = Integer.MAX_VALUE;
int index;
int v;
String line;
String[] lineArr;
int fromIndex = 0;
int[] temp;
while (in.hasNextLine()) { // 注意 while 处理多个 case
if (linecnt == 1) {
cnt = in.nextInt();
array = new int[cnt][];
} else {
line = in.nextLine();
if ("".equals(line)) {
continue;
}
lineArr = line.split(" ");
index = Integer.parseInt(lineArr[0]);
if (index > array.length - 1) {
fromIndex = array.length - 1;
} else {
fromIndex = index;
}
v = Integer.parseInt(lineArr[1]);
for (int i = fromIndex; i >= 0; i--) {
temp = array[i];
if (temp == null) {
temp = new int[2];
temp[0] = index;
temp[1] = v;
array[i] = temp;
break;
}
if (temp[0] == index) {
temp[1] = temp[1] + v;
break;
} else {
temp[0] = temp[0] ^ index;
index = temp[0] ^ index;
temp[0] = temp[0] ^ index;
temp[1] = temp[1] ^ v;
v = temp[1] ^ v;
temp[1] = temp[1] ^ v;
}
}
}
if (linecnt > cnt) {
break;
}
linecnt ++;
}
for (int i = 0; i < array.length; i++) {
if (array[i] != null) {
System.out.println(array[i][0] + " " + array[i][1]);
}
}
}
}