PAT 1002 多项式相加

1002 A+B for Polynomials (25分)

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N1 a**N1 N2 a**N2 … N**K aNK

where K is the number of nonzero terms in the polynomial, N**i and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N**K<⋯<N2<N1≤1000.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5   

Sample Output:

3 2 1.5 1 2.9 0 3.2   

题目大意:

给定两个多项式的信息,让我们求两个多项式相加的结果。每一个多项式输入一行,第一个数字是多项式的项数 后面每两个数据一组为一个项,分别是指数和系数。 输入的数据按照 指数递减的顺序。

思路分析:

得益于输入的指数递减性,我们能够比较容易地模拟出多项式相加的过程。如果有了解归并排序的同学会发现,这个过程和归并排序的merge 操作有几分神似。

首先我们用一个Node类对象 保存多项式的每一项。并且一个多项式存放在一个list中。 然后用两个指针 i,j 分别遍历两个多项式。按照指数递增的方式产生结果。 需要注意的是,如果两个指数相同的多项式 系数互为相反数,那么,这两项是要被抵消的。 也就是不能在结果中出现。

可能有同学有更加巧妙的方法,比如在第二个多项式读入的时候,就开始进行求解。这样其实是更加节省空间和时间的。我下面的这种代码比较朴素,完整地进行了多项式相加的模拟。思维量稍稍小一些。

完整代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class  Main {
     
    static class Node {
     
        int exp;
        double cof;

        public Node(int exp, double cof) {
     
            this.exp = exp;
            this.cof = cof;
        }
    }

    public static void main(String[] args) throws IOException {
     
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] info1 = bf.readLine().split(" ");
        int count1 = Integer.parseInt(info1[0]);
        List<Node> poly1 = new ArrayList<>();
        for (int i = 1; i <= count1 * 2; ) {
     
            poly1.add(new Node(Integer.parseInt(info1[i++]), Double.parseDouble(info1[i++])));

        }
        String[] info2 = bf.readLine().split(" ");
        int count2 = Integer.parseInt(info2[0]);
        List<Node> poly2 = new ArrayList<>();
        for (int j = 1; j <= count2 * 2; ) {
     

            poly2.add(new Node(Integer.parseInt(info2[j++]), Double.parseDouble(info2[j++])));
        }

        List<Node> res = new ArrayList<>();
        int i = 0, j = 0;
        while (i < count1 && j < count2) {
     
            if (poly1.get(i).exp > poly2.get(j).exp) {
     

                res.add(new Node(poly1.get(i).exp, poly1.get(i).cof));
                i++;
            } else if (poly1.get(i).exp < poly2.get(j).exp) {
     
                res.add(new Node(poly2.get(j).exp, poly2.get(j).cof));
                j++;

            } else {
     
                if(poly1.get(i).cof  + poly2.get(j).cof !=0)
                    res.add(new Node(poly1.get(i).exp, poly1.get(i).cof + poly2.get(j).cof));
                i++;
                j++;
            }
        }

        while (i < count1) {
     
            res.add(new Node(poly1.get(i).exp, poly1.get(i).cof));
            i++;
        }
        while (j < count2) {
     
            res.add(new Node(poly2.get(j).exp, poly2.get(j).cof));
            j++;
        }

        System.out.print(res.size())  ;
        for(Node node  : res) {
     
            System.out.printf(" %d %.1f", node.exp, node.cof);
        }
    }
}

你可能感兴趣的:(PAT)