7-2 一元多項式的乘法與加法運算 (20 分)

明日可7發表於2018-10-01

7-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

這題本來用陣列模擬的,但是既然學了連結串列,就應該練習一下

程式碼;
 

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define LIST_INIT_SIZE 100000
#define LISTINCREMENT 10
#define mod 256
#define lowbit(x) (x&(-x))
#define mem(a,b) memset(a,b,sizeof(a))
#define FRER() freopen("in.txt","r",stdin);
#define FREW() freopen("out.txt","w",stdout);
using namespace std;
const int maxn = 10000 + 7;
typedef struct LNode {
    int a,b;
    struct LNode* next;
}LNode,*LinkList;
void CreateList(LinkList&L){
    int n;
    scanf("%d",&n);
    L =(LinkList)malloc(sizeof(LNode));
    LinkList p = L;
    L->next = NULL;
    while(n--){
        LinkList q;
        q = (LinkList)malloc(sizeof(LNode));
        scanf("%d%d",&q->a,&q->b);
        q->next = NULL;
        p->next = q;
        p = q;
    }
}
void print(LinkList&L){
    if(L->next==NULL) printf("0 0\n");
    else {
        int i=0;
        LinkList p = L->next;
        while(p){
            if(i++>0) printf(" ");
            printf("%d %d",p->a,p->b);
            p = p->next;
        }
        printf("\n");
    }
}
void AddList(LinkList&A,LinkList&B,LinkList&C){
    C = (LinkList)malloc(sizeof(LNode));
    C->next = NULL;
    LinkList pa,pb,pc;
    pa = A->next;
    pb = B->next;
    pc = C;
    while(pa&&pb){
        LinkList q = (LinkList)malloc(sizeof(LNode));
        if(pa->b==pb->b){
            q->a = pa->a+pb->a;
            q->b = pa->b;
            pa = pa->next;
            pb = pb->next;
        }else if(pa->b>pb->b){
            q->a = pa->a;
            q->b = pa->b;
            pa = pa->next;
        }else{
            q->a = pb->a;
            q->b = pb->b;
            pb = pb->next;
        }
//        cout<<q->a<<" "<<q->b<<endl;
        if(q->a!=0){
            pc->next = q;
            pc = q;
        }
    }
//    while(pb) {cout<<pb->a<<" "<<pb->b<<endl; pb = pb->next;}
    pc -> next = pa?pa:pb;
}
void InsertList(LinkList&L,int a,int b){
    LinkList q = (LinkList)malloc(sizeof(LNode));
    if(a==0) return;
    q->a = a;
    q->b = b;
    q->next = NULL;
    LinkList p = L;
    bool flag = false;
    while(p->next){
        if(p->next->b==b){
            p->next->a+=a;
            if(p->next->a==0){
                LinkList q=p->next;
                p->next = q->next;
                free(q);
            }
            flag = true;
            break;
        }
        if(p->next->b<b){
            q->next = p->next;
            p->next = q;
            flag = true;
            break;
        }
        p = p->next;
    }
    if(!flag){
        p -> next = q;
    }
}
void MulList(LinkList&A,LinkList&B,LinkList&C){
    LinkList pa,pb;
    pa = A->next;
    pb = B->next;
    while(pa){
        pb = B->next;
        while(pb){
            if(pa->a)
            InsertList(C, pa->a*pb->a, pa->b+pb->b);
            pb = pb->next;
        }
        pa = pa->next;
    }
}
int main(){
    LinkList A,B,C,D;
    D =(LinkList)malloc(sizeof(LNode));
    D->next = NULL;
    CreateList(A);
    CreateList(B);
    MulList(A, B, D);
    AddList(A, B, C);
    print(D);
    print(C);
}

 

相關文章