PTA数据结构-02-线性结构2 一元多项式的乘法与加法运算

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/hdu_ch/article/details/102726418

一、题目

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

二、解答

import java.util.*;

public class Main {
    public static void main(String[] args) {
        //指数为key,系数为value,存放成map
        HashMap<Integer,Integer> map1 = new HashMap<>();
        HashMap<Integer,Integer> map2 = new HashMap<>();
        Scanner sc = new Scanner(System.in);
        int len1 = sc.nextInt();
        for (int i = 0; i < len1 ; i++) {
            int value = sc.nextInt();
            int key = sc.nextInt();
            map1.put(key, value);
        }
        int len2 = sc.nextInt();
        for (int i = 0; i < len2 ; i++) {
            int value = sc.nextInt();
            int key = sc.nextInt();
            map2.put(key, value);
        }
        printMap(multiply(map1,map2));
        printMap(add(map1,map2));
    }
    public static HashMap<Integer,Integer> multiply(HashMap<Integer,Integer>map1, HashMap<Integer,Integer>map2){
        HashMap<Integer,Integer> res = new HashMap<>();
        for (int key1 : map1.keySet()) {
            for (int key2 : map2.keySet()){
                int newKey = key1+key2;
                int newValue = map1.get(key1) * map2.get(key2);
                if (res.containsKey(newKey)){
                    newValue = res.get(key1+key2) + newValue;
                    res.put(newKey,newValue);
                } else {
                    res.put(newKey,newValue);
                }
            }
        }
        return res;
    }
    public static HashMap<Integer,Integer> add(HashMap<Integer,Integer>map1, HashMap<Integer,Integer>map2){
        for (int key : map2.keySet()){
            if (map1.containsKey(key)){
                int newValue = map1.get(key) + map2.get(key) ;
                map1.put(key,newValue);
            } else {
                map1.put(key,map2.get(key));
            }
        }
        return map1;
    }

    public static void printMap(HashMap<Integer,Integer> map){
        ArrayList<Integer> keys = new ArrayList<>(map.keySet());
        Collections.sort(keys, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if (o1>o2) return -1;
                else if (o1<o2) return 1;
                else return 0;
            }
        });
        String res = "";
        for (int key : keys) {
            if (map.get(key)!=0) res = res + map.get(key) + " " + key + " ";
        }
        if (res.equals("")) res = "0 0";
        System.out.println(res.trim());
    }
}
  1. 解题思路:指数为key,系数为value,存放成map
  2. List可以直接用Collections的sort()方法进行排序,默认从小到大,此题重写了排序方式为从大到小
  3. 看清题干:全零多项式输出为0 0
  4. 如果只是一元多项式相加,可以考虑使用链表做

猜你喜欢

转载自blog.csdn.net/hdu_ch/article/details/102726418