【11】Add Two Polynomials
日期: 2020-10-04 分类: 跨站数据测试 352次阅读
2-1 Add Two Polynomials (20分)
Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node.
Format of functions:
Polynomial Add( Polynomial a, Polynomial b );
where Polynomial is defined as the following:
typedef struct Node *PtrToNode;
struct Node {
int Coefficient;
int Exponent;
PtrToNode Next;
};
typedef PtrToNode Polynomial;
/* Nodes are sorted in decreasing order of exponents.*/
The function Add is supposed to return a polynomial which is the sum of a and b.
中文解释:
2-1 2个多项式相加(20个)
写一个函数来加两个多项式。不要销毁输入。使用带虚拟头节点的链表实现。
注:零多项式由一个只有虚拟头节点的空列表表示。 其中多项式定义如下:
typedef struct Node *PtrToNode;
struct Node {
int Coefficient;
int Exponent;
PtrToNode Next; };
typedef PtrToNode Polynomial;
函数Add应该返回一个多项式,它是a和b的和。
Sample program of judge:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node {
int Coefficient;
int Exponent;
PtrToNode Next;
};
typedef PtrToNode Polynomial;
Polynomial Read(); /* details omitted */
void Print( Polynomial p ); /* details omitted */
Polynomial Add( Polynomial a, Polynomial b );
int main()
{
Polynomial a, b, s;
a = Read();
b = Read();
s = Add(a, b);
Print(s);
return 0;
}
/* Your function will be put here */
Sample Input:
4
3 4 -5 2 6 1 -2 0
3
5 20 -7 4 3 1
Sample Output:
5 20 -4 4 -5 2 9 1 -2 0
AC代码
思路:
方法一:先对两个链表进行指数的比较,把较大的那一个存入一个新的链表,如果相等,就把两个节点对应的系数相加,再存入新的链表里。(如果相加之后为0,直接舍弃该项)。
可以这样子做的原因我也想了:
该题目给的数据都是按照指数降序排列的。如果没有,还要先合并同类项,再排列,复杂很多。
方法二:将这些数据逐一存储到数组中,对数组进行操作会更简单。
(PS:我猜的,没有做,但是毕竟数组什么不能做,嘿嘿)
(缝缝补补)终于通过了!
后来我把代码简化了一下,因为原来真的太多了,代码重复率太高,现在看着舒服多了。
Polynomial Add( Polynomial a, Polynomial b ) {
//我当成了带有头节点的链表来写的
if(a->Next==NULL)
return b;
if(b->Next==NULL)
return a;
Polynomial L=(Polynomial)malloc(sizeof(struct Node));
L->Next=NULL;
Polynomial p,q,s,t;
t=L;
p=a->Next;
q=b->Next;
while(p&&q) {
s=(Polynomial)malloc(sizeof(struct Node));
if(p->Exponent>q->Exponent) {
s->Coefficient=p->Coefficient;
s->Exponent=p->Exponent;
p=p->Next;
} else if(p->Exponent<q->Exponent) {
s->Coefficient=q->Coefficient;
s->Exponent=q->Exponent;
q=q->Next;
} else {
s->Coefficient=p->Coefficient+q->Coefficient;
s->Exponent=p->Exponent;
p=p->Next;
q=q->Next;
if(s->Coefficient==0)
//系数为0时,该结果将不再多项式内显示
continue;
}
s->Next=NULL;
t->Next=s;
t=s;
}
/*一下两种情况都要进行判断的,
因为上面可能是同时到链表尾部*/
if(p)
t->Next=p;
if(q)
t->Next=q;
return L;
}
下面这个是自己敲得完整代码:
(题目给的数据测试通过了)
也许会有个别的极端数据依旧不可以。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node
{
int Coefficient;
int Exponent;
PtrToNode Next;
};
typedef PtrToNode Polynomial;
Polynomial Read(); /* details omitted */
void Print( Polynomial p ); /* details omitted */
Polynomial Add( Polynomial a, Polynomial b );
int main()
{
Polynomial a, b, s;
a = Read();
b = Read();
s = Add(a, b);
Print(s);
return 0;
}
/* Your function will be put here */
Polynomial Add( Polynomial a, Polynomial b ) {
//我当成了带有头节点的链表来写的
if(a->Next==NULL)
return b;
if(b->Next==NULL)
return a;
Polynomial L=(Polynomial)malloc(sizeof(struct Node));
L->Next=NULL;
Polynomial p,q,s,t;
t=L;
p=a->Next;
q=b->Next;
while(p&&q) {
s=(Polynomial)malloc(sizeof(struct Node));
if(p->Exponent>q->Exponent) {
s->Coefficient=p->Coefficient;
s->Exponent=p->Exponent;
p=p->Next;
} else if(p->Exponent<q->Exponent) {
s->Coefficient=q->Coefficient;
s->Exponent=q->Exponent;
q=q->Next;
} else {
s->Coefficient=p->Coefficient+q->Coefficient;
s->Exponent=p->Exponent;
p=p->Next;
q=q->Next;
if(s->Coefficient==0)
continue;
}
s->Next=NULL;
t->Next=s;
t=s;
}
/*一下两种情况都要进行判断的,
因为上面可能是同时到链表尾部*/
if(p)
t->Next=p;
if(q)
t->Next=q;
return L;
}
Polynomial Read()//构造一个带有头节点的链表
{
Polynomial Head,p,s;
int N;
int i,x,y;
Head=(struct Node*)malloc(sizeof(struct Node));
Head->Next=NULL;//头节点首先置为空
s=Head;//任何时候头节点不可以改变,但是可以改变指向头节点的指针
scanf("%d",&N);
for(i=0; i<N; i++)
{
scanf("%d %d",&x,&y);
p=(struct Node*)malloc(sizeof(struct Node));
p->Coefficient=x;
p->Exponent=y;
s->Next=p;
s=p;
}
s->Next=NULL;
return Head;
}
void Print( Polynomial p )
{
Polynomial s=p;
while(s->Next)
{
s=s->Next;
if(s->Next==NULL)
printf("%d %d",s->Coefficient,s->Exponent);
else
printf("%d %d ",s->Coefficient,s->Exponent);
}
}
除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog
精华推荐