数据表记录包含表索引index和数值value(int范围的正整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照index值升序进行输出。
提示:
0 <= index <= 11111111
1 <= value <= 100000
先输入键值对的个数n(1 <= n <= 500)
接下来n行每行输入成对的index和value值,以空格隔开
输出合并后的键值对(多行)
4 0 1 0 2 1 2 3 4
0 3 1 2 3 4
3 0 1 0 2 8 9
0 3 8 9
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)); } }
Scanner in = new Scanner(System.in); var n = in.nextLine(); var line=Integer.parseInt(n); if (line<=0||line > 500) { System.out.println("超过输入范围,0~500"); return; } Map<Integer, Integer> map = new TreeMap<>(); for (int i = 0; i < line; i++) { var str = in.nextLine(); var strArr = str.split(" "); if (strArr.length != 2) { System.out.println("输入格式有误"); return; } var index = Integer.parseInt(strArr[0]); var value = Integer.parseInt(strArr[1]); if (index < 0 || index > 11111111) { System.out.println("index超过输入范围,0~111111"); } if (value<0||value>100000){ System.out.println("value超过输入范围,0~100000"); } if (map.containsKey(index)) { map.put(index, map.get(index) + value); } else { map.put(index, value); } } map.forEach((k, v) -> System.out.println(k + " " + v));