91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
阿城市| 敦煌市| 湘阴县| 隆子县| 旌德县| 嘉善县| 黄浦区| 浦城县| 曲周县| 葵青区| 湖北省| 天门市| 鲁山县| 双江| 信丰县| 昌都县| 三穗县| 石屏县| 泸西县| 松潘县| 芮城县| 化州市| 楚雄市| 翁牛特旗| 临高县| 安达市| 闽清县| 怀远县| 建平县| 太康县| 东海县| 公安县| 永州市| 富平县| 宁阳县| 卓资县| 堆龙德庆县| 和田县| 聊城市| 丁青县| 本溪|