Fork me on GitHub

单链表的使用——计算多项式加法

Problem Description

jhljx是一名学习特别差的学生,尤其在数学方面非常不擅长。。像计算类的问题从来算不对。。这天,上初中的jhljx走在回家的路上.......
郁闷ing......
<img:/richtext/res/1074/5e1932eff707503.jpg>
他突然想起老师给他布置的一个作业,让他计算多项式的加法。
居然还没做。。
<img:/richtext/res/1075/2.jpg>

给出的两个多项式的项数分别为n和m。两个多项式为P1(x)=A0+A1*x+A2*x^2+……+An-1*x^(n-1)+An*x^n,P2(x)=B0+B1*x+B2*x^2+……+Bn-1*x^(n-1)+Bn*x^n。
请你计算两个多项式相加的结果。

Input

输入多组数据。
对于每组数据,第一行输入两个数n(0<=n<=500)和m(0<=m<=500),分别表示两个多项式的最高次数。
第二行为n+1个整数(可以为负数),分别表示第一个多项式的系数。
第三行为m+1个整数(可以为负数),分别表示第二个多项式的系数。
保证系数从0次项,1次项,2次项……n次项(m次项)的顺序给出。

Output

输出最终的多项式。详细输出格式见样例。

Sample Input

14 18
1 0 0 0 0 0 -10 0 2 0 0 0 0 0 7
0 0 0 0 -1 0 10 0 0 0 -3 0 0 0 8 0 0 0 4

Sample Output

1-x^4+2x^8-3x^10+15x^14+4x^18

Hint

jhljx:不会敲?

<img:/richtext/res/1076/1.jpg>

***:额。。还不是很熟嘛。。

<img:/richtext/res/1077/2.jpg>

jhljx:见书上P66页
***:谢啦。。
jhljx:书上代码有错。。请谨慎使用。。
***:

<img:/richtext/res/1078/1.jpg>

请用链表实现,不要偷工减料。
题目来源:http://acm.buaa.edu.cn/problem/1201/
下面给出链表操作代码:(关键在于输入阶段的判断,建议自己写代码并提交)
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cmath>
  5 #include<memory.h>
  6 #define maxsize 510
  7 int c[maxsize],d[maxsize],e[maxsize];
  8 using namespace std;
  9 typedef struct Term
 10 {
 11     float coef;
 12     int exp;
 13     struct Term *link;
 14 } List,*Polynomial;
 15 void initTerm(Polynomial& first)
 16 {
 17     first=new List;
 18     if(!first)
 19     {
 20         cerr<<"存储分配失败\n";
 21     }
 22     first->link=NULL;
 23 }
 24 int main()
 25 {
 26     int m,n,mnum;
 27     while(cin>>m>>n)
 28     {
 29         int counter=0;
 30         memset(c,0,sizeof(c));
 31         memset(d,0,sizeof(d));
 32         memset(e,0,sizeof(e));
 33         if(n>m)mnum=n;
 34         else mnum=m;
 35         Polynomial firsts,firstp,s,p,prs,prp;
 36         initTerm(firsts);
 37         initTerm(firstp);
 38         prs=firsts;
 39         prp=firstp;
 40         for(int i=0; i<=m; i++)
 41         {
 42             s=new List;
 43             s->link=prs->link;
 44             prs->link=s;
 45             prs=s;
 46             s->exp=i;
 47             cin>>s->coef;
 48         }
 49         for(int i=0; i<=n; i++)
 50         {
 51             p=new List;
 52             p->link=prp->link;
 53             prp->link=p;
 54             prp=p;
 55             p->exp=i;
 56             cin>>p->coef;
 57         }
 58         float a[510];
 59         if(m>=n)
 60         {
 61             for(int i=0; i<=n; i++)
 62             {
 63                 firstp=firstp->link;
 64                 firsts=firsts->link;
 65                 c[i]=firstp->coef+firsts->coef;
 66             }
 67 
 68             for(int i=n+1; i<=m; i++)
 69             {
 70                 firsts=firsts->link;
 71                 c[i]=firsts->coef;
 72             }
 73         }
 74         if(m<n)
 75         {
 76             for(int i=0; i<=m; i++)
 77             {
 78                 firstp=firstp->link;
 79                 firsts=firsts->link;
 80                 c[i]=firstp->coef+firsts->coef;
 81             }
 82             for(int i=m+1; i<=n; i++)
 83             {
 84                 firstp=firstp->link;
 85                 c[i]=firstp->coef;
 86             }
 87          }
 88            for(int i=0; i<=mnum; i++)
 89         {
 90             if(c[i]!=0)
 91             {
 92                 d[counter]=c[i];
 93                 e[counter]=i;
 94                 counter++;
 95             }
 96         }
 97 //        for(int i=0;i<counter;i++)
 98 //            cout<<d[i]<<" ";
 99 //        cout<<endl;
100 //        for(int i=0;i<counter;i++)
101 //            cout<<e[i]<<" ";
102 //            cout<<endl;
103         if(e[0]==0)
104         {
105             cout<<d[0];
106         }
107         else if(e[0]==1)
108         {
109             if(d[0]>0)
110             {
111                 if(d[0]==1)
112                     cout<<"x";
113                 else
114                     cout<<d[0]<<"x";
115             }
116             if(d[0]<0)
117             {
118                 if(d[0]==-1)
119                     cout<<"-x";
120                 else
121                     cout<<d[0]<<"x";
122             }
123         }
124         else
125         {
126             if(d[0]>0)
127             {
128                 if(d[0]==1)
129                     cout<<"x^"<<e[0];
130                 else
131                     cout<<d[0]<<"x^"<<e[0];
132             }
133             if(d[0]<0)
134             {
135                 if(d[0]==-1)
136                     cout<<"-x^"<<e[0];
137                 else
138                     cout<<d[0]<<"x^"<<e[0];
139             }
140         }
141         for(int i=1; i<counter; i++)
142         {
143             if(e[i]==1)
144             {
145                 if(d[i]>0)
146                 {
147                     if(d[i]==1)
148                         cout<<"+x";
149                     else
150                         cout<<"+"<<d[i]<<"x";
151                 }
152                 if(d[i]<0)
153                 {
154                     if(d[i]==-1)
155                         cout<<"-x";
156                     else
157                         cout<<d[i]<<"x";
158                 }
159             }
160             else
161             {
162                 if(d[i]>0)
163                 {
164                     if(d[i]==1)
165                         cout<<"+"<<"x^"<<e[i];
166                     else
167                         cout<<"+"<<d[i]<<"x^"<<e[i];
168                 }
169                 if(d[i]<0)
170                 {
171                     if(d[i]==-1)
172                         cout<<"-x^"<<e[i];
173                     else
174                         cout<<d[i]<<"x^"<<e[i];
175                 }
176             }
177         }
178         cout<<endl;
179     }
180 }

下面给出纯数组求解过程:

  1 //2014级数据结构第二次上机 - jhljx计算多项式
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<memory.h>
  7 #define maxsize 510
  8 int a[maxsize],b[maxsize],c[maxsize],d[maxsize],e[maxsize];
  9 //a[maxsize]表示第一个多项式各项系数,
 10 //b[maxsize]表示第二个多项式各项系数
 11 //c[maxsize]表示相加后多项式各项系数
 12 //d[maxsize]表示系数不为0的
 13 //e[maxsize]表示系数不为0的项的次数
 14 using namespace std;
 15 
 16 int main()
 17 {
 18     int n,m,mnum;
 19     while(cin>>n>>m)
 20     {
 21         int counter=0;
 22         memset(a,0,sizeof(a));
 23         memset(b,0,sizeof(b));
 24         memset(c,0,sizeof(c));
 25         memset(d,0,sizeof(d));
 26         memset(e,0,sizeof(e));
 27         if(n>m)mnum=n;
 28         else mnum=m;
 29         for(int i=0; i<=n; i++)
 30             cin>>a[i];
 31         for(int j=0; j<=m; j++)
 32             cin>>b[j];
 33         for(int t=0; t<=mnum; t++)
 34             c[t]=a[t]+b[t];
 35         for(int i=0; i<=mnum; i++)
 36         {
 37             if(c[i]!=0)
 38             {
 39                 d[counter]=c[i];
 40                 e[counter]=i;
 41                 counter++;
 42             }
 43         }
 44 //        for(int i=0;i<counter;i++)
 45 //            cout<<d[i]<<" ";
 46 //        cout<<endl;
 47 //        for(int i=0;i<counter;i++)
 48 //            cout<<e[i]<<" ";
 49 //            cout<<endl;
 50         if(e[0]==0)
 51         {
 52             cout<<d[0];
 53         }
 54         else if(e[0]==1)
 55         {
 56             if(d[0]>0)
 57             {
 58                 if(d[0]==1)
 59                     cout<<"x";
 60                 else
 61                     cout<<d[0]<<"x";
 62             }
 63             if(d[0]<0)
 64             {
 65                 if(d[0]==-1)
 66                     cout<<"-x";
 67                 else
 68                     cout<<d[0]<<"x";
 69             }
 70         }
 71         else
 72         {
 73             if(d[0]>0)
 74             {
 75                 if(d[0]==1)
 76                     cout<<"x^"<<e[0];
 77                 else
 78                     cout<<d[0]<<"x^"<<e[0];
 79             }
 80             if(d[0]<0)
 81             {
 82                 if(d[0]==-1)
 83                     cout<<"-x^"<<e[0];
 84                 else
 85                     cout<<d[0]<<"x^"<<e[0];
 86             }
 87         }
 88         for(int i=1; i<counter; i++)
 89         {
 90             if(e[i]==1)
 91             {
 92                 if(d[i]>0)
 93                 {
 94                     if(d[i]==1)
 95                         cout<<"+x";
 96                     else
 97                         cout<<"+"<<d[i]<<"x";
 98                 }
 99                 if(d[i]<0)
100                 {
101                     if(d[i]==-1)
102                         cout<<"-x";
103                     else
104                         cout<<d[i]<<"x";
105                 }
106             }
107             else
108             {
109                 if(d[i]>0)
110                 {
111                     if(d[i]==1)
112                         cout<<"+"<<"x^"<<e[i];
113                     else
114                         cout<<"+"<<d[i]<<"x^"<<e[i];
115                 }
116                 if(d[i]<0)
117                 {
118                     if(d[i]==-1)
119                         cout<<"-x^"<<e[i];
120                     else
121                         cout<<d[i]<<"x^"<<e[i];
122                 }
123             }
124         }
125         cout<<endl;
126     }
127 }
View Code

 



posted @ 2015-11-23 18:42  伊甸一点  阅读(775)  评论(0编辑  收藏  举报