zoukankan      html  css  js  c++  java
  • PTA7-1 一元多项式的乘法与加法运算(Java实现)

    7-1 一元多项式的乘法与加法运算 (20 分)

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

    输入格式:

    输入分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 {
    static class Term implements Comparable<Term> {
    public int a; //系数
    public int b; //指数

    public Term(int a, int b) {
    this.a = a;
    this.b = b;
    }

    public Term() {
    }

    @Override
    public int compareTo(Term newTerm) {
    return Integer.compare(this.b, newTerm.b);
    }
    }

    public static void main(String[] args) {
    Stack<Term> stack0 = new Stack<>(); //中转栈
    Stack<Term> stack = new Stack<>(); //式子1_乘法栈
    Stack<Term> stack1 = new Stack<>(); //式子2_加法栈
    Stack<Term> stack2 = new Stack<>(); //式子1_乘法栈
    Queue<Term> queue = new LinkedList<>(); //式子2_乘法队列
    Queue<Term> queue1 = new LinkedList<>(); //乘法结果
    Queue<Term> queue2 = new LinkedList<>(); //加法结果
    ArrayList<Term> mul = new ArrayList<>();
    ArrayList<Term> addTerm = new ArrayList<>();

    Scanner in = new Scanner(System.in);
    int n1 = in.nextInt();
    for (int i = 0; i < n1; i++) {
    Term term = new Term(in.nextInt(), in.nextInt());
    stack2.push(term);
    stack0.push(term);
    }
    while (stack0.empty() == false) {
    stack.push(stack0.pop());
    }
    int n2 = in.nextInt();
    for (int i = 0; i < n2; i++) {
    Term term = new Term(in.nextInt(), in.nextInt());
    queue.offer(term); //插入队列
    stack0.push(term);
    }
    while (stack0.empty() == false) {
    stack1.push(stack0.pop());
    }

    int flag = n2;
    /**
    * 乘法:将式子1的各项保存在栈stack2中,将式子2的各项保存在队列queue中,让stack中的各项与queue中的各项相乘,结果保存在queue1中,
    * 同时将queue1中的该项移到队尾,stack2中的该项出栈,知道stack2为空时,乘法运算完成。完成之后需要将queue1中为0的项删除。
    */
    while (!stack2.empty()) {
    n2 = flag;
    Term term0 = stack2.peek();
    while (n2-- > 0) {
    Term term1 = queue.poll();
    Term term2 = new Term();
    term2.a = term0.a * term1.a;
    term2.b = term0.b + term1.b;
    queue1.offer(term2); //乘法结果队列
    queue.offer(term1);
    }
    stack2.pop();
    }
    while (queue1.peek() != null) {
    if (queue1.peek().a != 0) //去除队列中系数为0的项,将不为0的项加入到mul列表中
    mul.add(queue1.poll());
    else {
    queue1.poll();
    }
    }

    /**
    * 为了方便控制输出,将queue1中的各项放进列表mul中,放的过程中将列表中指数相同的项进行合并,
    * 列表中保存的对象就是正确的输出。
    */
    for (int i = 0; i < mul.size(); i++) {
    for (int j = 0; j < mul.size(); j++) {
    if (mul.get(i).b == mul.get(j).b && i != j) {
    mul.get(i).a += mul.get(j).a;
    if (mul.get(i).a == 0) { //删除合并后为0的项
    mul.remove(Math.max(j, i));
    mul.remove(Math.min(j, i));
    } else {
    mul.remove(j);
    }
    }
    }
    }

    /**
    * 加法:stack和stack1都是被逆转过的栈(栈顶元素指数最大)。将栈stack的栈顶元素和栈stack1的栈顶元素作比较,若相等,那么系数相加,指数不变,将这个结果放进
    * 队列queue2中,同时让stack和stack1中的栈顶元素出栈。如果栈stack的栈顶元素和栈stack1的栈顶元素不相等,那么让系数较大的栈顶元素出栈,进入队列queue2。如果stack
    * 和stack1中某一个栈为空,那么将非空的那个栈的所有元素都放进队列queue2中。最后将系数不为0的各项放入列表addTerm中。
    */

    while (stack.empty() == false) {
    Term term0 = stack.peek();
    if (stack1.empty() == false) {
    Term term3 = stack1.peek();
    if (term3.b == term0.b) {
    Term term4 = new Term();
    term4.a = term0.a + term3.a;
    term4.b = term0.b;
    queue2.offer(term4);
    stack.pop();
    stack1.pop();
    } else {
    if (Math.max(term0.b, term3.b) == term3.b) { //让系数较大的项入队
    queue2.offer(term3);
    stack1.pop();
    } else {
    queue2.offer(term0);
    stack.pop();
    }
    }
    } else {
    queue2.offer(term0);
    stack.pop();
    }
    }
    while (stack1.empty() == false) {
    queue2.offer(stack1.pop());
    }
    while (queue2.peek() != null) {
    if (queue2.peek().a != 0)
    addTerm.add(queue2.poll());
    else {
    queue2.poll();
    }
    }
    /**
    * 输出:将乘法列表mul排序后输出。如果mul和addTerm中某一个列表为空,那么输出“0 0”
    */
    if (mul.size() > 0) {
    Collections.sort(mul);
    for (int i = mul.size() - 1; i > 0; i--) {
    if (mul.get(i).a != 0 && mul.get(i - 1).a != 0)
    System.out.print(mul.get(i).a + " " + mul.get(i).b + " ");
    else if (mul.get(i).a != 0 && mul.get(i - 1).a == 0)
    System.out.println(mul.get(i).a + " " + mul.get(i).b);
    }
    if (mul.get(0).a != 0)
    System.out.println(mul.get(0).a + " " + mul.get(0).b);
    } else {
    System.out.println("0 0");
    }

    if (addTerm.size() > 0) {
    for (int i = 0; i < addTerm.size() - 1; i++) {
    if (addTerm.get(i).a != 0 && addTerm.get(i + 1).a != 0)
    System.out.print(addTerm.get(i).a + " " + addTerm.get(i).b + " ");
    else if (addTerm.get(i).a != 0 && addTerm.get(i + 1).a == 0)
    System.out.print(addTerm.get(i).a + " " + addTerm.get(i).b);
    }
    if (addTerm.get(addTerm.size() - 1).a != 0)
    System.out.print(addTerm.get(addTerm.size() - 1).a + " " + addTerm.get(addTerm.size() - 1).b);
    } else {
    System.out.print("0 0");
    }
    }
    }




     
  • 相关阅读:
    iOS8.1 To iOS8.2
    OAuth授权验证说明
    iOS消息推送
    Swift2
    Swift1
    数据结构,每次看都有不一样的体会
    修改CS、IP的指令(学习汇编)
    C,C++中的堆与栈
    12864显示菜单处理编程
    C编程常见的内存错误
  • 原文地址:https://www.cnblogs.com/yangyalong/p/11227575.html
Copyright © 2011-2022 走看看