数据表记录包含表索引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.*; import java.lang.*; import java.text.*; import java.math.*; import java.util.stream.Collectors; import static java.util.Map.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int num = sc.nextInt(); Map<Integer,Integer> map=new HashMap(); for(int i=0;i<num;i++){ int key = sc.nextInt(); int value = sc.nextInt(); map.put(key,map.getOrDefault(key,0)+value); } List<Entry<Integer, Integer>> collect = map.entrySet().stream().sorted((o1, o2)->{ return o1.getKey() - o2.getKey(); } ).collect(Collectors.toList()); for (Entry<Integer, Integer> entry : collect) { System.out.println(entry.getKey()+" "+entry.getValue()); } } sc.close(); } }
#include<stdio.h> struct data{ //构造结构体类型 int index; int value; }; int main(void) { struct data temp[500]; //创建结构体数组 int n; scanf("%d",&n);//输入个数n for(int i=0;i<n;i++) { scanf("%d %d",&temp[i].index,&temp[i].value);//输入下标值与内容值 } //选择排序,将具有相同下标值的结构体数组元素变为相邻的 for(int i=0;i<n-1;i++) { int min=i,j; for(j=i+1;j<n;j++) { if(temp[min].index>temp[j].index) { min=j; } } if(min!=j) { struct data ll=temp[i]; temp[i]=temp[min]; temp[min]=ll; } } /*将相邻的结构体数组元素的下标值进行比较,若相同则将下标大的内容值加到小标小 的元素中,再将下标大的元素的下标值置为 -1*/ for(int k=n-1;k>0;k--) { if(temp[k].index==temp[k-1].index) { temp[k-1].value+=temp[k].value; for(int j=k;j<n-1;j++) { temp[k]=temp[k+1]; } temp[k].index=-1; } } //遍历输出数组元素,注意判断其下标值是否为 -1,不是就输出,否则不输出 for(int i=0;i<n;i++) { if(temp[i].index!=-1) printf("%d %d\n",temp[i].index,temp[i].value); } return 0; }
运用一个数学上的小技巧:
#include #define MAX 500 #define long long size_t int cmp(size_t *a, size_t *b){ return (int)(*a - *b); } int main(){ int num = 0, temp1 = 0, temp2 = 0, size = 0, flag = 0; scanf("%d\n",&num); size_t *key = malloc(num*sizeof(size_t)); int *value = malloc(num*sizeof(int)); memset(key,-1,num*sizeof(size_t)); for(int i=0; i<num; i++){ flag = 1; scanf("%d %d\n",&temp1,&temp2); for(int a=1; a<=size; a++){ if(temp1 == key[a]/MAX){ value[a] += temp2; flag = 0; } } if(flag){ size++; key[size] = (temp1*MAX)+size; value[size] = temp2; } } qsort(key,size+1,sizeof(size_t),cmp); for(int j = 1; j<=size; j++){ printf("%d %d\n",key[j]/MAX,value[key[j]%MAX]); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Map; import java.util.TreeMap; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = br.readLine()) != null) { TreeMap<Integer, Integer> map = new TreeMap<>(); for (int i = 0; i < Integer.parseInt(s); i++) { String[] kv = br.readLine().split(" "); int key = Integer.parseInt(kv[0]), value = Integer.parseInt(kv[1]); map.put(key, map.getOrDefault(key, 0) + value); } for (Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } } } }
#输入键值对个数 n = int(input()) l1 = [] l2 = [] #键值分别出存在l1和l2中 for i in range(n): a,b=input().split() a = int(a) b = int(b) #如果键重复,则对应的值做加法运算 if a in l1: d = l1.index(a) l2[d] += b else: l1.append(a) l2.append(b) #将新的键值对合并到新的列表l中,排序后输出 l = list(zip(l1,l2)) l.sort() for each in l: print(each[0],each[1])
// 循环取出每一个键值个数 // 新建一个map { key: value, key:value }, // 判断map是否有这个key,有key的话,value相加,没有就塞进map中 // 最终结果 取出所有的key,对key进行排列, 按照key值从map中取出value输出 function sys() { let map = new Map(); var line = []; while(line=readline()) { var arr = line.split(' '); if (arr.length == 2) { if (map.has(arr[0])) { var val = map.get(arr[0]); map.set(arr[0], Number(val) + Number(arr[1])); } else { map.set(arr[0], arr[1]); } } } var keysArr = []; for (var s of map.keys()) { arrKeys.push(s); } keysArr.sort((a,b) => {return a - b}); keysArr.map((item) => { console.log(item + ' ' + map.get(item)); }) } sys();
public static void main(String[] args) { TreeMap<Integer, Integer> treeMap = new TreeMap<>(); Scanner sc = new Scanner(System.in); int num = Integer.parseInt(sc.nextLine()); while (num>0) { String str = sc.nextLine(); String[] mapStr = str.split(" "); Integer key = Integer.valueOf(mapStr[0]); Integer value = Integer.valueOf(mapStr[1]); if (!treeMap.containsKey(key)) { treeMap.put(key, value); } else { treeMap.put(key, treeMap.get(key) + value); } num--; } for (Map.Entry<Integer, Integer> entry : treeMap.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } }
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int nums = sc.nextInt(); Map<Integer,Integer> map = new TreeMap<>(); for(int i=0;i<nums;i++){ int index = sc.nextInt(); int value = sc.nextInt(); map.merge(index,value, Integer::sum); } map.forEach((k,v)->{ System.out.println(k+" "+v); }); } }
#include <bits/stdc++.h> using namespace std; int main() { // map:有序,红黑树 适用性:对于顺序要求高的问题,适用map会更高效点 //unordered_map:无序,哈希表 适用性:对于查找问题更加高效 map<int, int> hash; int n; cin >> n; while(n--) { int a, b; cin >> a >> b; hash[a] += b; } for (auto& v : hash) { cout << v.first << " " << v.second << endl; } return 0; }
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); while(scan.hasNext()) { int num=scan.nextInt(); Map<Integer,Integer> map=new TreeMap<Integer,Integer>(); for(int i=0;i<num;i++) { int key=scan.nextInt(); int value=scan.nextInt(); for(Integer j:map.keySet()) { if(key==j) { value=value+map.get(j); } } map.put(key,value); } for(Map.Entry<Integer,Integer> entry:map.entrySet()) { System.out.println(entry.getKey()+" "+entry.getValue()); } } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); Map<Integer, Integer> result = new TreeMap<Integer, Integer>(); int pairs = 0; if (scanner.hasNextInt()){ pairs = scanner.nextInt(); } for (int i = 0; i < pairs; i++){ int idx = scanner.nextInt(); int value = scanner.nextInt(); if (result.containsKey(idx)){ result.put(idx,result.get(idx) + value); } else{ result.put(idx,value); } } for (Integer key : result.keySet()){ System.out.printf("%d %d%n",key, result.get(key)); } } }
#include <iostream> #include <map> using namespace std; int main() { int n; map<int,int> m; cin>>n; while(n--) { int key,vaule; cin>>key>>vaule; m[key]=m[key]+vaule; } for(auto i=m.begin();i!=m.end();i++) cout<<i->first<<" "<<i->second<<endl; return 0; }}
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int size = scanner.nextInt(); if (size < 1) { continue; } TreeMap<Integer, Integer> treeMap = new TreeMap<>(); for (int i = 0; i <= size; i++) { String line = scanner.nextLine().trim().replaceAll("\\s", " "); String[] arr = line.split(" "); if (arr.length != 2) { continue; } try { int first = Integer.parseInt(arr[0]); int second = Integer.parseInt(arr[1]); if(treeMap.containsKey(first)){ int newValue = treeMap.get(first) + second; treeMap.put(first, newValue); } else { treeMap.put(first, second); } } catch (NumberFormatException e) { continue; } } treeMap.forEach((key, value) -> System.out.println(key + " " + value)); } }
#include<iostream> #include<vector> using namespace std; int main() { int NumberCount=0; while(cin>>NumberCount) { vector<int> Number(NumberCount,0); int Index=0,Value=0; for(int i=0;i<NumberCount;i++) { cin>>Index; cin>>Value; Number[Index]=Number[Index]+Value; } for(int i=0;i<NumberCount;i++) { if(Number[i]>0) { cout<<i<<" "<<Number[i]<<endl; } } } return 0; }