中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

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
弥渡县| 茂名市| 隆安县| 诏安县| 克东县| 定州市| 义马市| 广南县| 白玉县| 唐河县| 博客| 德江县| 南京市| 梁平县| 绍兴市| 扶绥县| 高阳县| 榆树市| 孝感市| 武山县| 修水县| 德格县| 巴青县| 工布江达县| 长阳| 呼伦贝尔市| 明光市| 桐梓县| 准格尔旗| 大方县| 延庆县| 格尔木市| 武邑县| 米泉市| 璧山县| 岳西县| 黄冈市| 二手房| 平乐县| 涿州市| 保康县|