第一行输入一个整数
代表数据表的记录数。
此后
行,第
行输入两个整数
代表数据表的第
条记录。
输出若干行,第
行输出两个整数,代表合并后数据表中第
条记录的索引和数值。
4 0 1 0 2 1 2 3 4
0 3 1 2 3 4
在这个样例中,第
条记录索引相同,合并数值为
。
2 0 1 0 1
0 2
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]); } } } }
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNext()) { int num = input.nextInt(); //定义map树,定义key-value TreeMap<Integer, Integer> map = new TreeMap(); int index, value; for (int i = 0; i < num; i++) { index = input.nextInt(); value = input.nextInt(); //判断index是否已经存在,若存在,value相加,所不存在,向map中添加index,value if (map.containsKey(index)) { map.put(index, map.get(index) + value); } else map.put(index, value); } //输出 for (Map.Entry<Integer, Integer>entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } } } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); Integer num = in.nextInt(); Map<Integer,Integer> map = new TreeMap<>(); for (int i = 0; i < num; i++) { if (i == 0) { map.put(in.nextInt(),in.nextInt()); } else { Integer one = in.nextInt(); Integer two = in.nextInt(); if (map.containsKey(one)) { map.put(one,map.get(one) + two); } else { map.put(one,two); } } } for (Integer key : map.keySet()) { System.out.println(key + " " + map.get(key)); } }