首页 > 试题广场 >

合并表记录

[编程题]合并表记录
  • 热度指数:801146 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}数据表中,一条记录包含表索引和数值两个值。请对表索引相同的记录进行合并(即将相同索引的数值进行求和运算),随后按照索引值的大小从小到大依次输出。

输入描述:
\hspace{15pt}第一行输入一个整数 n\left(1 \leqq n \leqq 500\right) 代表数据表的记录数。
\hspace{15pt}此后 n 行,第 i 行输入两个整数 x_i, y_i\left(0 \leqq x_i \leqq 11\,111\,111;\ 1 \leqq y_i \leqq 10^5\right) 代表数据表的第 i 条记录的索引和数值。


输出描述:
\hspace{15pt}一共若干行(视输入数据变化),第 i 行输出两个整数,代表合并后数据表中第 i 条记录的索引和数值。
示例1

输入

4
0 1
0 2
1 2
3 4

输出

0 3
1 2
3 4

说明

\hspace{15pt}在这个样例中,第 1,2 条记录索引相同,合并数值为 1 + 2 = 3
示例2

输入

2
0 1
0 1

输出

0 2
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();
    }
}

发表于 2025-06-18 13:46:15 回复(0)
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());
        }
    }
}

发表于 2025-05-15 00:14:02 回复(0)
我佛了 只要 创建一个容量大小为11111111的数组就oom
import java.util.HashMap;
import java.util.Scanner;

/**
 * @author xuzheng
 * @project PACKAGE_NAME
 * @data 2025/4/27
 */
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int[] arr=new int[11111111];
        for (int i = 0; i < n; i++) {
            int a=sc.nextInt();
            int b=sc.nextInt();
            arr[a]+=b;
        }
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=0){
                System.out.println(i+" "+arr[i]);
            }
        }

    }



}


发表于 2025-04-27 23:47:30 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = Integer.parseInt(in.nextLine());
            Map<Integer, Integer> map = new TreeMap<>();
            for (int i = 0; i < n; i++) {
                String[] arr = in.nextLine().split(" ");
                int key = Integer.parseInt(arr[0]);
                int value = Integer.parseInt(arr[1]);
                if (map.containsKey(key)) {
                    map.put(key, map.get(key) + value);
                } else {
                    map.put(key, value);
                }
            }
            map.forEach((key, value) -> {
                System.out.println(key + " " + value);
            });
        }
    }
}
发表于 2025-04-02 15:25:25 回复(0)
使用TreeMap自动排序,当然也可以使用TreeSet,但是需要先创建一个数据结构并且实现comparable接口
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));
        }
    }

}


发表于 2025-02-11 15:15:00 回复(0)
使用哈希表,最后把key单独拉出来做个排序,然后按图索骥挨个输出就行。
97ms,15364KB
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));
        }
    }
}


发表于 2024-11-27 23:02:38 回复(0)
排行里那些用数组实现的代码是怎么测过的?index比个数大不报错吗?
发表于 2024-11-03 16:34:44 回复(0)
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();
    }
}

发表于 2024-10-18 23:05:31 回复(0)
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);
        Map<Integer,Integer> map = new TreeMap<>();
        while (in.hasNextInt()) {
            int num = in.nextInt();
            for(int i = 0;i < num;i++){
                int key = in.nextInt();
                int value = in.nextInt();
                if(map.keySet().contains(key)){
                    map.put(key,map.get(key)+value);
                }else{
                    map.put(key,value);
                }
            }
        }
        map.keySet().forEach(key ->{
            System.out.println(key+" "+map.get(key));
        });
    }
}
发表于 2024-09-12 13:52:33 回复(0)
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));
        }
    }
}

发表于 2024-09-11 21:49:43 回复(0)
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 count = in.nextInt();
            Map<Integer, Integer> m =  new TreeMap<>();
            for (int i=0; i<count; i++) {
                int index = in.nextInt();
                int value = in.nextInt();
                if (m.containsKey(index)) {
                    m.put(index, value + m.get(index));
                } else {
                    m.put(index, value);
                }
            }
        for (Map.Entry<Integer, Integer> entry : m.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + " " + value);
        }

        }
    }
}
发表于 2024-09-06 15:09:40 回复(0)
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));
        }
    }
}

发表于 2024-07-02 21:27:24 回复(1)
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]);
            }
        }
    }
}

发表于 2024-06-19 06:37:22 回复(0)
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();
        int[][] index = new int[12][1000000];
        while(n>0){
            int i = in.nextInt();
            int v = in.nextInt();
            index[i%12][i/12] += v;
            n--;
        }
        for(int i=0;i<=11111111;i++){
            if(index[i%12][i/12]!=0){
                System.out.println(i+" "+index[i%12][i/12]);
            }
        }
    }
}
发表于 2024-05-23 16:16:28 回复(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());
            }
        }
    }
}

发表于 2024-05-15 22:33:44 回复(0)

问题信息

难度:
278条回答 107984浏览

热门推荐

通过挑战的用户

查看代码
合并表记录