PTA數據結構5-1,5-2

5-1 最大子列和問題

P.S:最大子列問題,原來就寫過,很多地方也有,包括算法導論的時候也寫了,而且這一題只需輸出最大的和,也就更簡單了,我的博客中也發發過噢,請參考   最大子列問題

5-2 一元多項式的乘法與加法運算

設計函數分別求兩個一元多項式的乘積與和。

輸入格式:

輸入分2行,每行分別先給出多項式非零項的個數,再以指數遞降方式輸入一個多項式非零項係數和指數(絕對值均爲不超過1000的整數)。數字間以空格分隔。

輸出格式:

輸出分2行,分別以指數遞降方式輸出乘積多項式以及和多項式非零項的係數和指數。數字間以空格分隔,但結尾不能有多餘空格。

輸入樣例:
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

P.S:這個求一元多項式好像也是寫過的,加法的話還好,對三種不同情況下分析,然後還有兩個表分別爲空的情況就可以了,乘法的話,是蠻不好寫的,但是你自己分析一下之後,乘法就是通過加法組成的,所以需要兩個循環,就如將a中每一個元素分別與b中的所有元素相乘,兩個元素相乘是很好寫的,就是指數相加,係數相乘嘛,然後再累加嘛,運算一次加一次。其中還有一些空鏈的情況呀需要進行判斷分析。

code:

#include <stdio.h>
#include <stdlib.h>

using namespace std;

typedef int ElemtType;
typedef struct node *ptrNode;
typedef ptrNode LinkList;   //頭節點
typedef ptrNode Position;   //中間結點

LinkList creatList(int n);
LinkList list_add(LinkList a, LinkList b);
LinkList list_mul(LinkList a, LinkList b);
void printList(LinkList L);


struct node {
    ElemtType coefficient;
    ElemtType exponent;
    Position next;
};


int main()
{
    int n1,n2;
    LinkList L1,L2,L3,L4;

    scanf("%d",&n1);
    L1 = creatList(n1);
    scanf("%d",&n2);
    L2 = creatList(n2);

    L3 = list_add(L1,L2);
    L4 = list_mul(L1,L2);
    printList(L4);
    printf("\n");
    printList(L3);

    return 0;
}

/* 創建指定大小的鏈表 */
LinkList creatList(int n)
{
    LinkList head, r, p;    //定義頭節點
    int col, exp;           //係數和指數
    head = (LinkList)malloc(sizeof(struct node));   //生成頭節點
    r = head;

    while(n--) {
        scanf("%d %d", &col, &exp);
        p = (Position)malloc(sizeof(struct node));
        p->coefficient = col;
        p->exponent = exp;
        r->next = p;
        r = p;
    }
    r->next = NULL;
    return head;
}

// 兩鏈表相加
LinkList list_add(LinkList a, LinkList b)
{
    Position ha, hb;        //定義兩個指向中間節點的指針變量
    LinkList c, r, p;       //定義指向頭節點的指針變量
    int temp;       //臨時變量
    ha = a->next;
    hb = b->next;
    c = (LinkList)malloc(sizeof(struct node));      //分配一個節點的空間
    r = c;
    while(ha != NULL && hb != NULL)
    {
        p = (Position)malloc(sizeof(struct node));
        /* a的係數小於b的係數 */
        if(ha->exponent < hb->exponent) {
            /* p指向hb結點 */
            p->exponent = hb->exponent;
            p->coefficient = hb->coefficient;
            hb = hb->next;      //指針後移
            /* 加到c鏈表中 */
            r->next = p;
            r = p;
        }
        /* a的係數大於b的係數 */
        else if(ha->exponent > hb->exponent) {
            /* p指向ha結點 */
            p->exponent = ha->exponent;
            p->coefficient = ha->coefficient;
            ha = ha->next;      //指針後移
            /* 加到c鏈表中 */
            r->next = p;
            r = p;
        }
        /* ha, hb係數相同 */
        else {
            temp = ha->coefficient + hb->coefficient;
            p->coefficient = temp;
            if(temp!=0) {
                p->exponent = ha->exponent;
                p->coefficient = temp;
                r->next = p;
                r = p;
            }
            ha = ha->next;
            hb = hb->next;
        }
    }
    /* ha鏈表爲空,直接將b的元素複製到c中 */
    if(ha == NULL) {
        while(hb != NULL) {
            p = (Position)malloc(sizeof(struct node));
            p->coefficient = hb->coefficient;
            p->exponent = hb->exponent;
            hb = hb->next;
            r->next = p;
            r = p;
        }
    }
    /* hb鏈表爲空,直接將a的元素複製到c中 */
    if(hb == NULL) {
        while(ha != NULL) {
            p = (Position)malloc(sizeof(struct node));
            p->coefficient = ha->coefficient;
            p->exponent = ha->exponent;
            ha = ha->next;
            r->next = p;
            r = p;
        }
    }
    r->next = NULL;
    return c;
}

LinkList list_mul(LinkList a, LinkList b)
{
    Position ha, hb;                //定義兩個指向中間節點的指針變量
    LinkList c, r, p, tempC;        //定義指向頭節點的指針變量
    ha = a->next;                   //給ha, hb賦值
    hb = b->next;
    c = creatList(0);
    /* 如果a, b鏈表有一個爲空,直接返回c */
    if(ha == NULL || hb == NULL) {
        return c;
    }
    /* 當ha不爲空時 */
    while(ha!=NULL) {
        tempC = (LinkList)malloc(sizeof(struct node));      //創建一個結點
        r = tempC;
        hb = b->next;
        while(hb != NULL) {
            p = (LinkList)malloc(sizeof(struct node));
            /* a, b的兩結點相乘 */
            p->exponent = ha->exponent + hb->exponent;
            p->coefficient = ha->coefficient * hb->coefficient;

            hb = hb->next;
            r->next = p;
            r = p;
        }
        r->next = NULL;
        c = list_add(c, tempC);         //將c鏈表和tempC鏈表相加賦值給c
        ha = ha->next;
    }
    return c;
}

/* 打印結果,按要求輸出鏈表 */
void printList(LinkList L)
{
    LinkList hc;
    int flag = 0;

    hc = L->next;
    if(hc == NULL)
        printf("0 0");
    while(hc != NULL){
        if(flag)
            printf(" ");
        else
            flag = 1;
        printf("%d %d",hc->coefficient,hc->exponent);
        hc = hc->next;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章