noj 1006 多项式乘法

对于输入,由于是按指数递减输入的,故我们只需按照输入顺序进行存储即可。
对待一个输入:
1.如果当前多项式为空,那么将此项放入第一项
2.如果当前多项式不为空 

3.如果有指数相同的项,那么将此项与指数相同的项合并,如果系数变为0,则将此项删除。 
然后多项式乘法,即将第一个多项式的项和第二个多项式每一项分别相乘得到的项,新开辟一个链表存储结果。

最后把相乘得到的项合并同类项,新开辟一个链表,用下面的方法处理存入的项!

1.如果当前链表为空,那么将结果链表的第一项放入第一项
2.如果链表不为空 

3.如果有指数相同的项,那么将此项与指数相同的项合并,如果系数变为0,则将此项删除。 

4.如果没有指数相同的项(那么此项的指数一定比当前所有项的指数都小),那么将此项放入多项式末尾。
这样就可以合并了。 
    写的好揪心!!还好一次性过不然会改死!!

#include<iostream>
   #include<stdio.h>
   #include<malloc.h>
   using namespace std;
   struct node
   {
       int x,z;
       node *next;
       node(int a,int b):x(a),z(b),next(NULL){}
   };
void print(node *head)//按要求输出
{
       node *q=head->next;
       if(q==NULL)
         printf("0");
       else
       {
           int f=1,f2=0;
           while(q)
           {
               if(q==head->next)
               {
                   if(q->x!=1)
                   {
                       if(q->x==-1)
                            printf("-");
                       else
                           printf("%d",q->x);
                   }
               }
               else
               {
                   if(q->x>0)
                   {
                       printf("+");
                       if(q->x!=1)
                            printf("%d",q->x);
                   }
                    else
                    {
                        if(q->x!=-1)
                            printf("%d",q->x);
                        else
                            printf("-");
                    }
               }
               if(q->z!=0)
                    printf("X");
               else
               {
                   if(q->x==1||q->x==-1)
                        printf("1");
                   q=q->next;
                   continue;
               }
               if(q->z!=1)
                    printf("^%d",q->z);
               q=q->next;
           }
       }
        printf("\n");
}
   int main()
   {
       node *tail,*tail1,*tmp,*hea,*hea1,*hea2,*tail2,*tail3,*hea3;
       int n,m,f=1;
       tail=new node(0,0);
       while(scanf("%d%d",&n,&m)&&(n!=0||m!=-1))
       {
           if(n==0)
                continue;
           tmp=new node(n,m);
           if(tail->z==tmp->z)
             tail->x=tail->x+tmp->x;
           else
           {
             tail->next=tmp;
             tail=tmp;
           }
           if(f)
           {
               hea=new node(0,0);
               tmp->next=hea->next;
               hea->next=tmp;
           }
           f=0;
       }
       print(hea);
       f=1;
       tail1=new node(0,0);
       tail2=new node(0,0);
       while(scanf("%d%d",&n,&m)&&(n!=0||m!=-1))
       {
           if(n==0)
                continue;
           node *q=hea->next,*cur,*tmp1;
           int x,z;
           tmp1=new node(n,m);
           if(tail1->z==tmp1->z)
               tail1->x=tail1->x+tmp1->x;
           else
           {
               tail1->next=tmp1;
               tail1=tmp1;
           }
           if(f)
           {
               hea1=new node(0,0);
               tmp1->next=hea1->next;
               hea1->next=tmp1;
           }
           while(q)
           {
               x=q->x*n;
               z=q->z+m;
               cur=new node(x,z);
               tail2->next=cur;
               tail2=cur;
               if(f)
               {
                   hea2=new node(0,0);
                   cur->next=hea2->next;
                   hea2->next=cur;
               }
               q=q->next;
               f=0;
           }
       }
           node *p=hea2->next,*tmp3;
           tail3=new node(0,0);
           hea3=new node(0,0);
           f=1;
           while(p)
           {
               if(hea3==NULL)
               {
                   tmp3=new node(p->x,p->z);
                   tail3->next=tmp3;
                   tail3=tmp3;
                   if(f)
                   {
                       tmp3->next=hea3->next;
                       hea3->next=tmp3;
                   }
               }
               else
               {
                    node *p1=hea3,*q1=hea3->next,*cur1,*cur2;
                    int f1=0;
                    while(q1)
                    {
                       if(p->z==q1->z)
                       {
                           f1=1;
                           q1->x=q1->x+p->x;
                           if(q1->x==0)
                           {
                               p1->next=q1->next;
                               delete q1;
                               q1=p1->next;
                           }
                           break;
                       }
                       if(p->z>q1->z)
                       {
                           f1=1;
                           cur1=new node(p->x,p->z);
                           p1->next=cur1;
                           cur1->next=q1;
                           break;
                       }
                       p1=p1->next;
                       q1=p1->next;
                    }
                    if(!f1)
                    {
                        cur2=new node(p->x,p->z);
                        p1->next=cur2;
                        cur2->next=NULL;
                    }
               }
               p=p->next;
           }
       print(hea1);
       print(hea3);
       return 0;
   }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值