美文网首页
Java 一元多项式的乘法与加法运算

Java 一元多项式的乘法与加法运算

作者: 向祥祥 | 来源:发表于2020-03-30 16:10 被阅读0次

问题

求两个一元多项式的乘积与和。

输入格式:输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数。数字间以空格分隔。

输出格式:输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。相乘或相加结果为0,则输出: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

计算代码

public static void main(String[] args) {
    TreeMap<Integer,Integer> polynomial1=new TreeMap<Integer, Integer>();
    TreeMap<Integer,Integer> afterSummation=new TreeMap<Integer, Integer>();
    TreeMap<Integer,Integer> afterMultiplication=new TreeMap<Integer, Integer>();
    Scanner sc=new Scanner(System.in);
    int index=sc.nextInt();
    for(int i=1;i<=index;i++) {
        int coefficient=sc.nextInt();
        int indices=sc.nextInt();
        polynomial1.put(indices, coefficient);
        afterSummation.put(indices, coefficient);
    }
    index=sc.nextInt();
    for(int i=1;i<=index;i++) {
        int coefficient=sc.nextInt();
        int indices=sc.nextInt();
        polynomialSummationOnePolynomial(afterSummation,indices,coefficient);
        polynomialSummationMultiplePolynomial(afterMultiplication,polynomialMultiplicationOne(polynomial1,indices,coefficient));
    }
    sc.close();
    System.out.println(printHashMap(afterMultiplication));
    System.out.println(printHashMap(afterSummation));
}
public static String printHashMap(TreeMap<Integer,Integer> needPrint) {
    String str="";
    for(Entry<Integer,Integer> entry:needPrint.entrySet()) {
        if(entry.getValue()!=0) {
            str=" "+entry.getValue()+" "+entry.getKey()+str;
        }
    }
    if(str.equals("")) {
        str="0 0";
    }
    return str.trim();
}
public static TreeMap<Integer,Integer> polynomialMultiplicationOne(TreeMap<Integer,Integer> needMultiplication,int indices,int coefficient){
    TreeMap<Integer,Integer> afterMultiplication=new TreeMap<Integer, Integer>();
    for(Entry<Integer, Integer> entry:needMultiplication.entrySet()) {
        afterMultiplication.put(entry.getKey()+indices, entry.getValue()*coefficient);
    }
    return afterMultiplication;
}
public static void polynomialSummationMultiplePolynomial(TreeMap<Integer,Integer> afterSummation,TreeMap<Integer,Integer> needSummation){
    for(Entry<Integer, Integer> entry:needSummation.entrySet()) {
        if(afterSummation.containsKey(entry.getKey())) {
            afterSummation.put(entry.getKey(), afterSummation.get(entry.getKey())+entry.getValue());
        }else {
            afterSummation.put(entry.getKey(), entry.getValue());
        }
    }
}
public static void polynomialSummationOnePolynomial(TreeMap<Integer,Integer> afterSummation,int indices,int coefficient){
    if(afterSummation.containsKey(indices)) {
        afterSummation.put(indices, afterSummation.get(indices)+coefficient);
    }else{
        afterSummation.put(indices, coefficient);
    }
}

相关文章

  • 线性结构-相关算法

    编译环境:python v3.5.0, mac osx 10.11.4 多项式加法与乘法运算(队列) 主要思路: ...

  • 02-线性结构2 一元多项式的乘法与加法运算(20 分)

    传送门 中国大学MOOC-陈越、何钦铭-数据结构-2017秋 题目 02-线性结构2 一元多项式的乘法与加法运算(...

  • Java 一元多项式的乘法与加法运算

    问题 求两个一元多项式的乘积与和。 输入格式:输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一...

  • 一元多项式

    1 、问题描述: 功能:设计一个一元多项式加法器。 输入并建立多项式,实现两个多项式的加法运算。 要求: 1) 界...

  • JS 加、减、乘、除运算避免浮点数

    加法运算 减法运算 乘法运算 除法运算

  • 2018-03-16

    JAVA - 长数据运算 使用数组实现长数据加法、乘法的简单模拟 使用数组,模拟类似竖式运算的模式,逐位进行运算与...

  • 《运算律》教学反思

    运算律包括加法交换律、加法结合律、乘法交换律、乘法结合律、乘法分配律。这些运算律在数与运算中起这重要的作用。通过回...

  • 为什么说除法是乘法的逆运算

    为什么说除法是乘法的逆运算 其实除法是乘法的逆运算与减法是加法的逆运算类似,不同之处在于:加法逆运算的表达是通过...

  • DS_ 一元多项式的乘法与加法运算

    在PTA上刷DS的题目,有些问题和细节,放上来和大家分享和讨论 设计函数分别求两个一元多项式的乘积与和。 输入格式...

  • 乘法先于加法?

    我们在学与教时都强调:乘法是求连续几个相同加数的和;乘法是加法的简便运算,虽没有说加法在前,乘法在后,但大家...

网友评论

      本文标题:Java 一元多项式的乘法与加法运算

      本文链接:https://www.haomeiwen.com/subject/smpvuhtx.html