多项式乘法

来源:互联网 发布:美国航空航天局数据库 编辑:程序博客网 时间:2024/06/05 18:09
题目的链接为:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1006
题目为:
多项式乘法
时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte
总提交:171            测试通过:77

描述


线性表是一种最简单、最基本,也是最常用的数据结构,其用途十分广泛,例如,用带表头结点的单链表求解一元整系数多项式加法和乘法运算。

现给两个一元整系数多项式,请求解两者的乘积。


输入


两组数据,每一组代表一个一元整系数多项式,有多行组成,其中每一行给出多项式每一项的系数和指数,这些行按指数递减次序排序。每一组结束行输入为0 -1


输出


三组数据,前两组为一元整系数多项式,最后一组为两个多项式的乘积。

一元整系数多项式输出形式如下:

(1)多项式项4x输出为4X

(2)多项式项4x2输出为4X^2

(3)第一项系数为正数时,加号不要输出

(4)除常系数项外,项系数为1不显式输出,-1输出为-

例如,4x3- x2+x-1正确输出形式为4X^3-X^2+X-1,错误输出形式为 +4X^3-1X^2+1X-1


样例输入

3 14
-8 8
6 2
2 0
0 -1
2 10
4 8
-6 2
0 -1

样例输出

3X^14-8X^8+6X^2+2
2X^10+4X^8-6X^2
6X^24+12X^22-16X^18-50X^16+12X^12+76X^10+8X^8-36X^4-12X^2

刚做完多项式加法,趁热打铁做多项式乘法。其实乘法就是遍历两个多项式,将系数相乘,指数相加:
如果有指数相同的项,那么合并;
如果没有指数相同的项,那么按照指数递减的顺序找到合适的位置存放该项目。
最后再输出。
C++代码 复制代码
  1. #include<iostream>   
  2. using namespace std;   
  3. typedef struct node{   
  4.          
  5.       int xs;   
  6.       int zs;   
  7.       struct node *next;     
  8. }Node;   
  9. int xs,zs;   
  10. bool tag=true;   
  11. void print(Node *root)   
  12. {   
  13.     Node *p=root->next;   
  14.     while(p!=NULL)   
  15.     {   
  16.          if(p->xs==0)   
  17.          {   
  18.              p=p->next;   
  19.              continue;    
  20.          }   
  21.          if(p==root->next)   
  22.          {   
  23.               if(p->xs!=1)   
  24.               {   
  25.                  if(p->xs==-1)   
  26.                  {   
  27.                      cout<<"-";    
  28.                  }       
  29.                  else  
  30.                  {   
  31.                      cout<<p->xs;    
  32.                  }   
  33.               }    
  34.          }    
  35.          else  
  36.          {   
  37.               if(p->xs>0)   
  38.               {   
  39.                    cout<<"+";   
  40.                    if(p->xs!=1)   
  41.                    {   
  42.                        cout<<p->xs;    
  43.                    }   
  44.               }    
  45.               else  
  46.               {   
  47.                     if(p->xs!=-1)   
  48.                     {   
  49.                        cout<<p->xs;   
  50.                     }    
  51.                     else  
  52.                     {   
  53.                        cout<<"-";    
  54.                     }   
  55.               }   
  56.          }   
  57.          if(p->zs!=0)   
  58.          {   
  59.                cout<<"X";   
  60.          }   
  61.          else  
  62.          {   
  63.                if(p->xs==1||p->xs==-1)   
  64.                {   
  65.                   cout<<"1";    
  66.                }   
  67.                   
  68.                p=p->next;   
  69.                continue;    
  70.          }   
  71.          if(p->zs!=1)   
  72.          {   
  73.                cout<<"^"<<p->zs;    
  74.          }   
  75.             
  76.          p=p->next;   
  77.     }   
  78.     cout<<endl;   
  79. }   
  80. int main(){   
  81.   
  82.     Node *root[2],*root1,*t,*u,*k,*e1,*e2;   
  83.     for(int i=0;i<2;i++){   
  84.         root[i]=NULL;    
  85.     }    
  86.     root1=new Node();   
  87.     for(int i=0;i<2;i++){   
  88.         while(cin>>xs>>zs&&(xs!=0||zs!=-1)){   
  89.             
  90.             if(xs==0)   
  91.             {   
  92.                continue;   
  93.             }   
  94.             if(root[i]==NULL)   
  95.             {   
  96.                   Node *temp=new Node();   
  97.                   temp->xs=xs;   
  98.                   temp->zs=zs;   
  99.                   temp->next=NULL;   
  100.                      
  101.                   root[i]=new Node();   
  102.                   root[i]->next=temp;   
  103.             }   
  104.             else  
  105.             {   
  106.                   Node *p,*q;   
  107.                   p=root[i]->next;   
  108.                   q=root[i];   
  109.                   while(p!=NULL&&p->zs!=zs)   
  110.                   {   
  111.                        q=p;   
  112.                        p=p->next;    
  113.                   }   
  114.                   if(p==NULL)   
  115.                   {   
  116.                        Node *temp=new Node();   
  117.                        temp->xs=xs;   
  118.                        temp->zs=zs;   
  119.                        temp->next=NULL;   
  120.                      
  121.                        q->next=temp;   
  122.                   }   
  123.                   else  
  124.                   {   
  125.                        p->xs+=xs;   
  126.                        if(p->xs==0)   
  127.                        {   
  128.                            q->next=p->next;    
  129.                        }   
  130.                   }   
  131.             }   
  132.         }   
  133.         print(root[i]);     
  134.     }   
  135.     t=root[0]->next;   
  136.     u=root[1]->next;   
  137.     k=root1;   
  138.       
  139.     while(t!=NULL)   
  140.     {   
  141.        u=root[1]->next;   
  142.        while(u!=NULL)   
  143.        {   
  144.            int tempXS=t->xs*u->xs;   
  145.            int tempZS=t->zs+u->zs;   
  146.            if(tempXS==0)   
  147.            {   
  148.                u=u->next;   
  149.                continue;   
  150.            }   
  151.            else  
  152.            {   
  153.                e2=root1->next;   
  154.                e1=root1;   
  155.                while(e2!=NULL&&e2->zs!=tempZS)   
  156.                {   
  157.                      e1=e2;   
  158.                      e2=e2->next;   
  159.                }   
  160.                if(e2==NULL)   
  161.                {   
  162.                      Node *tempNode=new Node();   
  163.                      tempNode->xs=tempXS;   
  164.                      tempNode->zs=tempZS;   
  165.                      tempNode->next=NULL;   
  166.                         
  167.                      //按照指数的大小放到合适的位置   
  168.                      e2=root1->next;   
  169.                      e1=root1;   
  170.                      while(e2!=NULL&&e2->zs>tempNode->zs)   
  171.                      {   
  172.                           e1=e2;   
  173.                           e2=e2->next;    
  174.                      }   
  175.                      e1->next=tempNode;   
  176.                      tempNode->next=e2;   
  177.                }   
  178.                else  
  179.                {   
  180.                      e2->xs+=tempXS;   
  181.                }   
  182.            }   
  183.            u=u->next;   
  184.        }     
  185.        t=t->next;    
  186.     }   
  187.     print(root1);   
  188.        
  189.     //释放new的空间    
  190.     for(int i=0;i<2;i++)   
  191.     {   
  192.         t=root[i];   
  193.         u=t;   
  194.         while(t!=NULL)   
  195.         {   
  196.               u=t->next;   
  197.               delete t;   
  198.               t=u;   
  199.         }    
  200.     }   
  201.     t=root1;   
  202.     u=t;   
  203.     while(t!=NULL)   
  204.     {   
  205.               u=t->next;   
  206.               delete t;   
  207.               t=u;   
  208.     }    
  209.     system("pause");   
  210.     return 0;   
  211. }  
原创粉丝点击