Java实现多项式相加与相乘

import java.util.Arrays;
import java.util.Scanner;

//数组实现多项式相加和相乘,并输出
// 4 3 4 -5 2 6 1 -2 0
// 3 5 20 -7 4 3 1
//输出:15 24 -25 22 30...
//      3 5 20 -7 4 3 1

public class Main {
    public static void main(String[] args) {
        int[] arr1 = readData();
        int[] arr2 = readData();
        int[] c = dataMul(arr1,arr2);
        int[] d = dataAdd(arr1,arr2);
        printArr(c);
        System.out.println();
        printArr(d);
    }

    //两个多项式的相加,并返回结果
    private static int[] dataAdd(int[] a, int[] b) {
        int[] arr = new int[a.length+b.length-1];
        int l = a[0] + b[0];
        //两数组相同项的个数,指数相同+1,系数和指数都相同+2
        int same = 0;
        int p = 1;  //a数组的下标
        int q = 1;  //b数组的下标
        int k = 1;  //arr数组的下标

        while(2*p<a.length && 2*q<b.length){
            if(a[2*p] > b[2*q]){
                arr[2*k-1] = a[2*p-1];
                arr[2*k] = a[2*p];
                p++;
                k++;
            }else if(a[2*p] < b[2*q]){
                arr[2*k-1] = b[2*q-1];
                arr[2*k] = b[2*q];
                q++;
                k++;
            }else{
                //指数系数都相等的情况
                if(a[2*p-1] == b[2*q-1]){
                    p++;
                    q++;
                    k++;
                    same += 2;
                //指数相等系数不等的情况
                }else{
                    arr[2*k-1] = a[2*p-1] + b[2*q-1];
                    arr[2*k] = a[2*p];
                    k++;
                    p++;
                    q++;
                    same++;
                }
            }
        }
        //说明a数组已遍历完,把b数组未处理项添加到arr数组后面
        while (2*p > a.length && 2*q < b.length){
            for (; 2*q < b.length; q++) {
                arr[2*k-1] = b[2*q-1];
                arr[2*k] = b[2*q];
                k++;
            }
        }
        //说明b数组已遍历完,把a数组未处理项添加到arr数组后面
        while (2*q > b.length && 2*p < a.length){
            for (; 2*p < a.length; p++) {
                arr[2*k-1] = a[2*p-1];
                arr[2*k] = a[2*p];
                k++;
            }
        }

        //两数组最后一项指数相同,系数也相同的情况
//        while (2*q > b.length && 2*p > a.length){}

        //为arr数组的第一项赋值
        arr[0] = l-same;
        arr = Arrays.copyOf(arr,2*k-1);

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

        //处理常数项为0
        int length = arr.length;
        if(arr[length-1]==0 && arr[length-2]==0){
            arr = Arrays.copyOf(arr,length-2);
        }
        return arr;
    }

    //返回多项式相乘的结果
    private static int[] dataMul(int[] a, int[] b) {
        //用数组a的每一项乘以数组b,结果用二维数组保存
        int[][] arr = new int[a[0]][b.length];

        //用于指向相乘结果后的数组
        int[] newArr;
//        int[] newArr = new int[2*a[0]+2*b[0]+1];

        int k = 1; //处理数组a第k组(系数与指数)
        int s = 1; //处理数组b时的临时下标
        while(k<=a[0]){
            arr[k-1][0] = b[0];
            for (int i = 1; 2*i < b.length; i++) {
                arr[k-1][2*i-1] = a[2*k-1]*b[2*i-1]; //系数相乘
                arr[k-1][2*i]   = a[2*k]+b[2*i]; //指数相加
            }
            k++;
        }

        if(arr.length == 1){
            return arr[0];
        }else{
            newArr = arr[0];
            for (int i = 1; i < arr.length; i++) {
                newArr = dataAdd(newArr,arr[i]);
            }
            return newArr;
        }
    }

    //从输入读取一行数据,并返回数组
    private static int[] readData() {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] split = s.replaceAll("( )+", " ").split(" ");
        int[] arr = new int[split.length];
        for(int i=0; i<split.length; i++){
            arr[i] = Integer.parseInt(split[i]);
        }
        return arr;
    }

    //打印数组的内容,数据间以空格间隔,为空打印:0 0
    private static void printArr(int[] c) {
        if(c[0] == 0){
            System.out.print("0 0");
        }else{
            for (int i = 1; i < c.length; i++) {
                if(i == 1){
                    System.out.print(c[i]);
                }else{
                    System.out.print(" "+c[i]);
                }
            }
        }
    }
}
 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于实现多项式的相加和相乘,可以使用单链表来存储多项式的每一项。 首先,我们可以创建一个节点类来表示多项式中的每一项: ```java class Node { int coefficient; // 系数 int exponent; // 指数 Node next; // 下一个节点 Node(int coefficient, int exponent) { this.coefficient = coefficient; this.exponent = exponent; this.next = null; } } ``` 然后,我们可以创建一个链表类来管理多项式的各个节点: ```java class Polynomial { Node head; Polynomial() { this.head = null; } // 添加节点 void addTerm(int coefficient, int exponent) { Node newNode = new Node(coefficient, exponent); if (head == null) { head = newNode; } else { Node temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; } } } ``` 接下来,我们可以实现多项式的相加和相乘的方法: ```java class PolynomialOperations { // 相加两个多项式 static Polynomial addPolynomials(Polynomial poly1, Polynomial poly2) { Polynomial result = new Polynomial(); Node p1 = poly1.head; Node p2 = poly2.head; while (p1 != null && p2 != null) { if (p1.exponent == p2.exponent) { result.addTerm(p1.coefficient + p2.coefficient, p1.exponent); p1 = p1.next; p2 = p2.next; } else if (p1.exponent > p2.exponent) { result.addTerm(p1.coefficient, p1.exponent); p1 = p1.next; } else { result.addTerm(p2.coefficient, p2.exponent); p2 = p2.next; } } while (p1 != null) { result.addTerm(p1.coefficient, p1.exponent); p1 = p1.next; } while (p2 != null) { result.addTerm(p2.coefficient, p2.exponent); p2 = p2.next; } return result; } // 相乘两个多项式 static Polynomial multiplyPolynomials(Polynomial poly1, Polynomial poly2) { Polynomial result = new Polynomial(); Node p1 = poly1.head; while (p1 != null) { Node p2 = poly2.head; while (p2 != null) { result.addTerm(p1.coefficient * p2.coefficient, p1.exponent + p2.exponent); p2 = p2.next; } p1 = p1.next; } return result; } } ``` 使用示例: ```java public class Main { public static void main(String[] args) { Polynomial poly1 = new Polynomial(); poly1.addTerm(2, 3); poly1.addTerm(1, 2); poly1.addTerm(3, 0); Polynomial poly2 = new Polynomial(); poly2.addTerm(1, 2); poly2.addTerm(2, 1); poly2.addTerm(4, 0); Polynomial sum = PolynomialOperations.addPolynomials(poly1, poly2); Polynomial product = PolynomialOperations.multiplyPolynomials(poly1, poly2); System.out.println("Sum of polynomials: "); printPolynomial(sum); System.out.println("Product of polynomials: "); printPolynomial(product); } // 打印多项式 static void printPolynomial(Polynomial poly) { Node temp = poly.head; while (temp != null) { System.out.print(temp.coefficient + "x^" + temp.exponent); if (temp.next != null) { System.out.print(" + "); } temp = temp.next; } System.out.println(); } } ``` 这样,我们就可以使用单链表实现多项式的相加和相乘了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值