PAT (Advanced Level) Practice1002 A+B for Polynomials (25 分)(Java实现)

全部答案(持续更新)

Problem Description

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 N 1 a N 1 N 2 a N 2 . . . N K a N K K N_1 a_{N_1} N_2 a_{N_2}... N_K a_{N_K}
​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 < < N 2 < N 1 1000 1≤K≤10,0≤N_K<⋯<N​_2​​ <N_​1≤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

代码示例(Java实现)

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

/**
 * 写的时候,一直没有想起来系数为 0 后,应该消去这一项
 * 
 * @create-date 2019-07-27 18:22
 */
public class Main {

    static class Node {
        double coefficient;
        int exponent;

        public Node (double coefficient, int exponent) {
            this.coefficient = coefficient;
            this.exponent = exponent;
        }

        @Override
        public String toString() {
            return exponent + " " + (coefficient == 0 ? 0 : String.format("%.1f", coefficient));
        }
    
    }

    public static void main(String[] args) throws IOException {
        Reader.init(System.in);
        List<Node> list1 = new ArrayList<>();
        List<Node> list2 = new ArrayList<>();
        int count1 = Reader.nextInt();
        for (int i = 0; i < count1; i++) {
            // 获取到指数 exponent
            int exponent = Reader.nextInt();
            // 获取到系数 coefficient
            double coefficient = Reader.nextDouble();
            // 创建一个结点,并添加到集合中
            list1.add(new Node(coefficient, exponent));
        }
        // 获取第二个多项式
        int count2 = Reader.nextInt();
        for (int i = 0; i < count2; i++) {
            // 获取到指数 exponent
            int exponent = Reader.nextInt();
            // 获取到系数 coefficient
            double coefficient = Reader.nextDouble();
            // 创建一个结点,并添加到集合中
            list2.add(new Node(coefficient, exponent));
        }
        // 创建一个集合来存储计算的结果
        List<Node> result = new ArrayList<>();
        int i = 0, j = 0;
        for (; i < list1.size() && j < list2.size();) {
            Node nodei = list1.get(i);
            Node nodej = list2.get(j);
            if (nodei.exponent > nodej.exponent) {
                result.add(nodei);
                i++;
            } else if (nodei.exponent == nodej.exponent) {
                result.add(new Node(nodei.coefficient + nodej.coefficient, nodei.exponent));
                i++;
                j++;
            } else {
                result.add(nodej);
                j++;
            }
        }
        while (i < list1.size()) {
            result.add(list1.get(i++));
        }
        while (j < list2.size()) {
            result.add(list2.get(j++));
        }
		
		// 将结果中系数为 0 的项删除掉
        List<Node> filterResult = new ArrayList<>();
        for (Node aResult : result) {
            if (aResult.coefficient != 0) {
                filterResult.add(aResult);
            }
        }
        System.out.print(filterResult.size());
        for (Node aResult : filterResult) {
            System.out.print(" " + aResult);
        }
    }

}

class Reader {

    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    public static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    public static String nextLine() throws IOException {
        return reader.readLine();
    }

    /** get next word */
    public static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

}

发布了80 篇原创文章 · 获赞 13 · 访问量 9259

猜你喜欢

转载自blog.csdn.net/qq_39424178/article/details/97960758
今日推荐