【问题标题】:Polynomial Multiplication Degree Math Incorrect with Linked Lists链表的多项式乘法度数数学不正确
【发布时间】:2015-09-29 03:53:38
【问题描述】:

我的乘法逻辑在这里的某个地方不正确。似乎我没有考虑何时必须添加从两个多项式相乘的结果中得到的相同次数的项。

public Polynomial multiply(Polynomial p) {
    if (p.poly == null || this.poly == null) {
        Polynomial zero = new Polynomial();
        zero.poly = new Node (0, 0, null);
        return zero;
    } else {

        Polynomial retPol = new Polynomial();
        retPol.poly = new Node(0, 0, null);

        Node front = retPol.poly;
        Node entered = p.poly;
        Node thisPol = this.poly;

        int high = Integer.MIN_VALUE;
        int low = Integer.MAX_VALUE;

        while (entered != null) {
            thisPol = this.poly; 

            while (thisPol != null) {
                if (thisPol.term.degree + entered.term.degree > high)
                    high = thisPol.term.degree + entered.term.degree;
                if (thisPol.term.degree + entered.term.degree < low)
                    low = thisPol.term.degree + entered.term.degree;

                thisPol = thisPol.next;
            }

            entered = entered.next;
        }

        entered = p.poly;

        Node create = front;
        for (int i = low; i <= high; i++) { 
            create.term.degree = i;
            create.term.coeff = 0;

            create.next = new Node (0, 0, null);
            create = create.next;
        }

        entered = p.poly;

        while (entered != null) {
            thisPol = this.poly; 

            while (thisPol != null) {
                int degree = entered.term.degree + thisPol.term.degree;
                create = front;

                while (create != null) {
                    if (create.term.degree == degree) {
                        create.term.coeff = entered.term.coeff * thisPol.term.coeff;
                    }

                    create = create.next;
                }

                thisPol = thisPol.next;
            }

            entered = entered.next;
        }

        create = front;

        while (create != null) {
            if (create.term.degree == high) {
                create.next = null;
                create = create.next;
            }
            else
                create = create.next;
        }

        retPol.poly = front;

        return retPol;
    }
}

我应该得到的答案是:

32.0x^9 + 16.0x^8 + -16.0x^7 + -20.0x^6 + 52.0x^5 + 38.0x^4 + -6.0x^3 + -6.0x^2 + 9.0x + 27.0

但我实际上得到了:

32.0x^9 + 16.0x^8 + -16.0x^7 + -8.0x^6 + 16.0x^5 + 24.0x^4 + 12.0x^3 + -6.0x^2 + -9.0x + 27.0

看来3度到6度的逻辑是不正确的。这是一个逻辑错误,我知道。我只是不确定如何解决。我也知道应该为那些不正确的度数添加谁的术语,但它看起来只是绕过它并且只显示一个。

任何提示将不胜感激。谢谢。

【问题讨论】:

    标签: java loops math linked-list logic


    【解决方案1】:

    我认为你应该总结所有对,而不是在这里选择最后一个(或随机)对:

                    if (create.term.degree == degree) {
                        create.term.coeff += entered.term.coeff * thisPol.term.coeff;
                    }
    

    [更新]您的代码经过我的更正后非常适合我:

    public class PolynomialTest {
        public static void main(String[] args) {
            // 4x^5 - 2x^3 + 2x + 3
            Polynomial p1 = new Polynomial(new Node(4.0, 5, new Node(-2.0, 3, new Node(2.0, 1, new Node(3.0, 0, null)))));
            // 8x^4 + 4x^3 - 3x + 9
            Polynomial p2 = new Polynomial(new Node(8.0, 4, new Node(4.0, 3, new Node(-3.0, 1, new Node(9.0, 0, null)))));
            Polynomial p3 = p1.multiply(p2);
            System.out.println(p3.toString());
        }
    }
    
    class Term {
        int degree;
        double coeff;
    }
    
    class Node {
        Term term;
        Node next;
        public Node(double coeff, int degree, Node next) {
            this.term = new Term();
            this.term.degree = degree;
            this.term.coeff = coeff;
            this.next = next;
        }
    }
    
    class Polynomial {
        private Node poly;
    
        public Polynomial() {}
    
        public Polynomial(Node poly) {
            this.poly = poly;
        }
    
        public Polynomial multiply(Polynomial p) {
            if (p.poly == null || this.poly == null) {
                Polynomial zero = new Polynomial();
                zero.poly = new Node (0, 0, null);
                return zero;
            } else {
    
                Polynomial retPol = new Polynomial();
                retPol.poly = new Node(0, 0, null);
    
                Node front = retPol.poly;
                Node entered = p.poly;
                Node thisPol = this.poly;
    
                int high = Integer.MIN_VALUE;
                int low = Integer.MAX_VALUE;
    
                while (entered != null) {
                    thisPol = this.poly; 
    
                    while (thisPol != null) {
                        if (thisPol.term.degree + entered.term.degree > high)
                            high = thisPol.term.degree + entered.term.degree;
                        if (thisPol.term.degree + entered.term.degree < low)
                            low = thisPol.term.degree + entered.term.degree;
    
                        thisPol = thisPol.next;
                    }
    
                    entered = entered.next;
                }
    
                entered = p.poly;
    
                Node create = front;
                for (int i = low; i <= high; i++) { 
                    create.term.degree = i;
                    create.term.coeff = 0;
    
                    create.next = new Node (0, 0, null);
                    create = create.next;
                }
    
                entered = p.poly;
    
                while (entered != null) {
                    thisPol = this.poly; 
    
                    while (thisPol != null) {
                        int degree = entered.term.degree + thisPol.term.degree;
                        create = front;
    
                        while (create != null) {
                            if (create.term.degree == degree) {
                                create.term.coeff += entered.term.coeff * thisPol.term.coeff;
                            }
    
                            create = create.next;
                        }
    
                        thisPol = thisPol.next;
                    }
    
                    entered = entered.next;
                }
    
                create = front;
    
                while (create != null) {
                    if (create.term.degree == high) {
                        create.next = null;
                        create = create.next;
                    }
                    else
                        create = create.next;
                }
    
                retPol.poly = front;
    
                return retPol;
            }
        }
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            Node n = poly;
            while (n != null) {
                if (sb.length() > 0)
                    sb.append(" + ");
                sb.append(n.term.coeff);
                if (n.term.degree > 0)
                    sb.append("x");
                if (n.term.degree > 1)
                    sb.append("^").append(n.term.degree);
                n = n.next;
            }
            return sb.toString();
        }
    }
    

    此代码完全符合您的预期:

    27.0 + 9.0x + -6.0x^2 + -6.0x^3 + 38.0x^4 + 52.0x^5 + -20.0x^6 + -16.0x^7 + 16.0x^8 + 32.0x^9
    

    【讨论】:

    • 没有运气是什么意思?数字错误?
    • 你想乘什么多项式?您只显示了没有输入的预期结果多项式。
    • 哎呀抱歉。 [4x^5 - 2x^3 + 2x + 3]*[8x^4 + 4x^3 - 3x + 9]
    • @qqxx,我刚刚检查过,一切似乎都运行良好。有关完整示例,请参阅我的更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 2015-09-25
    • 2013-08-12
    • 2022-12-10
    • 2014-01-25
    • 1970-01-01
    • 2020-05-03
    相关资源
    最近更新 更多