7-2 一元多项式的乘法与加法运算

思路:先算乘法,后算加法

PTA7-2 一元多项式的乘法与加法运算
测试样例:

序号    输入    输出
0    4 3 4 -5 2 6 1 -2 0    15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
      3 5 20 -7 4 3 1    5 20 -4 4 -5 2 9 1 -2 0
1    2 1 2 1 0    1 4 -1 0
      2 1 2 -1 0    2 2
2    2 -1000 1000 1000 0    -1000000 2000 2000000 1000 -1000000 0
      2 1000 1000 -1000 0    0 0
3    0    0 0
      1 999    1 999
————————————————
测试样例出处:版权声明:本文为CSDN博主「李柒岁」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_45955041/article/details/106996514

//纯链表
#include<stdio.h>
#include<stdlib.h>
struct link
{
    int coe;
    int exp;
    struct link *next;
};
int main()
{
    int n;
    int m;
    struct link *head1=NULL,*head2=NULL;
    struct link *rear=NULL;
    for(int i=0;i<2;i++)
    {
        rear=NULL;
        scanf("%d",&n);
        if(i==0) m=n;
        for(int j=0;j<n;j++)
        {
            struct link *p=(struct link*)malloc(sizeof(struct link));
            scanf("%d%d",&p->coe,&p->exp);
            if((i==0&&head1==NULL)||(i==1&&head2==NULL)) 
            {
                if(i==0) head1=rear=p;
                else head2=rear=p;
            }
            else
            {
                rear->next=p;
                rear=p;
            }
        }
        if(rear!=NULL) rear->next=NULL;//head1和head2的尾部分别设置
      //为NULL,这一步后面遍历要用到
    } 
    struct link *head3=NULL;
    struct link *node1=head1;
    rear=NULL;
    while(head1&&node1)
    {
        struct link *node2;
        if(head2!=NULL) node2=head2;
        while(head2&&node2)
        {
            struct link *now=(struct link*)malloc(sizeof(struct link));
            now->coe=node1->coe*node2->coe;
            now->exp=node1->exp+node2->exp;
            if(head3==NULL)  head3=rear=now;
            else 
            {
                if(rear->exp>now->exp) 
                {
                    rear->next=now;
                    rear=now;
                }
                //no problem!
                else 
                {
                    struct link *pre1=head3,*cur1=head3;
                    while(cur1&&cur1->exp>now->exp)
                    {
                        pre1=cur1;
                        cur1=cur1->next;
                    }
                    if(cur1->exp<now->exp) 
                    {
                        now->next=cur1;
                        pre1->next=now;
                    }
                    else if((cur1==rear)&&((now->coe+cur1->coe)==0))
                    {
                        pre1->next=NULL;
                        rear=pre1;
                    }
                    else 
                    {
                        if((now->coe+cur1->coe)==0) pre1->next=cur1->next;
                        else cur1->coe=now->coe+cur1->coe;
                    }
                }
            }
            node2=node2->next;
        }
        node1=node1->next;
    }
    if(m==0||n==0) 
    {
        struct link *result1=(struct link*)malloc(sizeof(struct link));
        result1->coe=0;
        result1->exp=0;
        rear=head3=result1;
    }
    rear->next=NULL;
    struct link* cur2=head3;
    int count1=0;
    while(cur2)
    {
        if(count1!=0) printf(" ");
        printf("%d %d",cur2->coe,cur2->exp);
        count1++;
        cur2=cur2->next;
    }
    printf("\n");
   //上面为乘法
   //下面为加法
    struct link *node3;
    if(head2!=NULL) node3=head2;
    while(head1&&head2&&node3)
    {
        struct link *temp=(struct link*)malloc(sizeof(struct link));
        temp->coe=node3->coe;
        temp->exp=node3->exp;
        node3=node3->next;
        if(temp->exp>head1->exp)
        {
            temp->next=head1;
            head1=temp;
        }
        else if((temp->exp==head1->exp))
        {
            if(head1->next!=NULL&&temp->coe+head1->coe==0) head1=head1->next;
            else if(head1->next==NULL&&(temp->coe+head1->coe==0))
            {
                head1->coe=0;
                head1->exp=0;
            }
            else head1->coe=temp->coe+head1->coe;
        }
        else
        {
            struct link *pre=head1,*cur=head1;
            while(cur&&cur->exp>temp->exp)
            {
                pre=cur;
                cur=cur->next;
            }
            if(cur==NULL) 
            {
                pre->next=temp;
                temp->next=NULL;
            }
            else 
            {
                if(cur->exp<temp->exp)
                {
                    temp->next=cur;
                    pre->next=temp;
                }
                else 
                {
                    if(cur->coe+temp->coe==0) pre->next=cur->next;
                    else cur->coe=cur->coe+temp->coe;
                }
            }
        }
    }
    if(head1) node1=head1;
    if(head2&&head1==NULL) node1=head2;
    if(head1==NULL&&head2==NULL) 
    {
        struct link *result2=(struct link*)malloc(sizeof(struct link));
        result2->coe=0;
        result2->exp=0;
        node1=head1=result2;
    }
    int count2=0;
    while(node1)
    {
        if(count2!=0) printf(" ");
        printf("%d %d",node1->coe,node1->exp);
        count2++;
        node1=node1->next;
    }
    return 0;
}

// 要注意的就是链表为不为空,不能访问空链表


//15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
//15 24 -21 8 -33 5 -15 3 18 2 -6 1
                                                               

                                                               

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值