線性結構2 一元多項式的乘法與加法運算


題目

02-線性結構2 一元多項式的乘法與加法運算(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

分析

使用鏈表保存多項式的系數和指數,因為輸入時是有序的,因此多項式的加類似於有序鏈表的排序,多項式的乘法可以在插入時確定要插入的位置,也可以固定插入到鏈表尾,最后對鏈表排序。

AC代碼

#include "bits/stdc++.h"
using namespace std;

typedef struct PloyNode *Ploynomial;

struct PloyNode
{
    int coef; //系數
    int expon; //指數
    Ploynomial link;
};

void Attach(int c, int e, Ploynomial *pRear) {  //因為要修改指針的值,因此需要傳入指針的指針
    Ploynomial p;
    p = (Ploynomial)malloc(sizeof(struct PloyNode));
    p->coef = c;
    p->expon = e;
    p->link = NULL;
    (*pRear)->link = p;
    *pRear = p; //修改pReal
}

Ploynomial ReadPloy() {
    Ploynomial p, Rear, t;
    int c, e, N;
    cin >> N;
    if (!N) return NULL;
    p = (Ploynomial)malloc(sizeof(struct PloyNode));
    p->link = NULL;
    Rear = p;
    while (N--) {
        cin >> c >> e;
        Attach(c, e, &Rear);//因為要修改指針的值,因此必須傳入地址
    }
    //刪除無用的頭結點
    t = p; p = p->link; free(t);
    return p;
}

Ploynomial Add(Ploynomial p1, Ploynomial p2) {
    if (!p1) return p2;
    if (!p2) return p1;
    Ploynomial t1, t2, p, Rear, t;
    t1 = p1; t2 = p2;
    p = (Ploynomial)malloc(sizeof(struct PloyNode));
    p->link = NULL;
    Rear = p;
    while (t1&&t2) {
        if (t1->expon == t2->expon) {
            int sum = t1->coef + t2->coef;
            if (sum) Attach(sum, t1->expon, &Rear);
            t1 = t1->link;
            t2 = t2->link;
        }
        else if (t1->expon > t2->expon) {
            Attach(t1->coef, t1->expon, &Rear);
            t1 = t1->link;
        }
        else {
            Attach(t2->coef, t2->expon, &Rear);
            t2 = t2->link;
        }
    }
    while (t1) {
        Attach(t1->coef, t1->expon, &Rear);
        t1 = t1->link;
    }
    while (t2) {
        Attach(t2->coef, t2->expon, &Rear);
        t2 = t2->link;
    }
    t = p; p = p->link; free(t);//刪除無用的頭結點
    return p;
}
Ploynomial Mult(Ploynomial p1, Ploynomial p2) {
    if (!p1 || !p2) return NULL;
    Ploynomial t1, t2, p, Rear, t;
    t1 = p1;
    p = (Ploynomial)malloc(sizeof(struct PloyNode));
    p->link = NULL;
    Rear = p;

    while (t1) {
        t2 = p2;
        while (t2) {
            Attach(t1->coef*t2->coef, t1->expon + t2->expon, &Rear);
            t2 = t2->link;
        }
        t1 = t1->link;
    }
    t = p; p = p->link; free(t);
    // 用冒泡把多項式按照指數降序排列
    t1 = p;
    while (t1->link) {
        t2 = p;
        while (t2->link) {
            if (t2->expon < t2->link->expon) {//交換位置
                swap(t2->expon, t2->link->expon);
                swap(t2->coef, t2->link->coef);
            }
            else if (t2->expon == t2->link->expon) {//指數相等,合並
                t = t2->link;
                t2->coef += t->coef;
                t2->link = t->link;
                free(t);
            }
            t2 = t2->link;
        }
        t1 = t1->link;
    }


    return p;
}

void PrintPloy(Ploynomial p) {
    if (!p) {
        cout << "0 0";
        return;
    }
    bool ans = true;
    if (p->coef) {
        cout << p->coef << ' ' << p->expon;
        ans = false;
    }
    p = p->link;
    while (p) {
        if (p->coef) {
            cout << ' ' << p->coef << ' ' << p->expon;
            ans = false;
        }
        p = p->link;
    }
    if (ans) cout << "0 0";
}
int main() {
    Ploynomial p1, p2, pp, ps;
    p1 = ReadPloy();
    p2 = ReadPloy();
    pp = Mult(p1, p2);
    PrintPloy(pp);
    cout << endl;
    ps = Add(p1, p2);
    PrintPloy(ps);

    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM