PTA数据结构5-1,5-2

来源:互联网 发布:c语言绝对公正的裁判 编辑:程序博客网 时间:2024/06/06 10:19

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;    }}
0 0
原创粉丝点击