亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

java怎么實現多項式相加

小億
98
2023-11-24 04:37:52
欄目: 編程語言

在 Java 中,可以使用數組或鏈表來表示多項式,然后通過遍歷多項式的每一項,將相同指數的項相加得到最終結果。

以下是一種使用鏈表表示多項式的實現方式:

public class PolynomialNode {
    int coefficient;
    int exponent;
    PolynomialNode next;
    
    public PolynomialNode(int coefficient, int exponent) {
        this.coefficient = coefficient;
        this.exponent = exponent;
        this.next = null;
    }
}

public class Polynomial {
    private PolynomialNode head;
    
    public Polynomial() {
        this.head = null;
    }
    
    public void addTerm(int coefficient, int exponent) {
        PolynomialNode newNode = new PolynomialNode(coefficient, exponent);
        
        if (head == null) {
            head = newNode;
        } else {
            PolynomialNode current = head;
            PolynomialNode previous = null;
            
            while (current != null && current.exponent > exponent) {
                previous = current;
                current = current.next;
            }
            
            if (current != null && current.exponent == exponent) {
                current.coefficient += coefficient;
            } else {
                newNode.next = current;
                
                if (previous != null) {
                    previous.next = newNode;
                } else {
                    head = newNode;
                }
            }
        }
    }
    
    public Polynomial add(Polynomial polynomial) {
        Polynomial result = new Polynomial();
        
        PolynomialNode node1 = this.head;
        PolynomialNode node2 = polynomial.head;
        
        while (node1 != null && node2 != null) {
            if (node1.exponent > node2.exponent) {
                result.addTerm(node1.coefficient, node1.exponent);
                node1 = node1.next;
            } else if (node1.exponent < node2.exponent) {
                result.addTerm(node2.coefficient, node2.exponent);
                node2 = node2.next;
            } else {
                int sum = node1.coefficient + node2.coefficient;
                
                if (sum != 0) {
                    result.addTerm(sum, node1.exponent);
                }
                
                node1 = node1.next;
                node2 = node2.next;
            }
        }
        
        while (node1 != null) {
            result.addTerm(node1.coefficient, node1.exponent);
            node1 = node1.next;
        }
        
        while (node2 != null) {
            result.addTerm(node2.coefficient, node2.exponent);
            node2 = node2.next;
        }
        
        return result;
    }
    
    public String toString() {
        StringBuilder sb = new StringBuilder();
        
        PolynomialNode current = head;
        boolean isFirstTerm = true;
        
        while (current != null) {
            if (current.coefficient != 0) {
                if (current.coefficient > 0 && !isFirstTerm) {
                    sb.append("+");
                }
                
                sb.append(current.coefficient);
                
                if (current.exponent > 1) {
                    sb.append("x^").append(current.exponent);
                } else if (current.exponent == 1) {
                    sb.append("x");
                }
                
                isFirstTerm = false;
            }
            
            current = current.next;
        }
        
        return sb.toString();
    }
}

使用示例:

public class Main {
    public static void main(String[] args) {
        Polynomial polynomial1 = new Polynomial();
        polynomial1.addTerm(3, 4);
        polynomial1.addTerm(2, 3);
        polynomial1.addTerm(5, 2);
        polynomial1.addTerm(1, 0);
        
        Polynomial polynomial2 = new Polynomial();
        polynomial2.addTerm(1, 3);
        polynomial2.addTerm(4, 2);
        polynomial2.addTerm(2, 1);
        polynomial2.addTerm(3, 0);
        
        Polynomial result = polynomial1.add(polynomial2);
        
        System.out.println("Polynomial 1: " + polynomial1);
        System.out.println("Polynomial 2: " + polynomial2);
        System.out.println("Result: " + result);
    }
}

輸出結果:

Polynomial 1: 3x^4+2x^3+5x^2+1
Polynomial 2: x^3+4x^2+2x+3
Result: 3x^4+3x^3+9x^2+2x+4

0
和林格尔县| 射洪县| 眉山市| 兴化市| 德清县| 石景山区| 那曲县| 奉节县| 惠安县| 化德县| 河北区| 南宁市| 平遥县| 通城县| 博乐市| 闽清县| 涟源市| 永德县| 兴隆县| 仙游县| 安溪县| 漠河县| 靖安县| 天等县| 平昌县| 彭州市| 安阳市| 平舆县| 安丘市| 钟山县| 新泰市| 宝山区| 沁源县| 奎屯市| 蓝山县| 康保县| 腾冲县| 兴和县| 新龙县| 积石山| 泸溪县|