多项式加法:
http://dsalgo.openjudge.cn/
描述:
我们经常遇到两多项式相加的情况,在这里,我们就需要用程序来模拟实现把两个多项式相加到一起。首先,我们会有两个多项式,每个多项式是独立的一行,每个多项式由系数、幂数这样的多个整数对来表示。
如多项式2x20- x17+ 5x9- 7x7+ 16x5+ 10x4 + 22x2- 15
对应的表达式为:2 20 -1 17 5 9 - 7 7 16 5 10 4 22 2 -15 0。
为了标记每行多项式的结束,在表达式后面加上了一个幂数为负数的整数对。
同时输入表达式的幂数大小顺序是随机的。
我们需要做的就是把所给的两个多项式加起来。
输入
输入包括多行。
第一行整数n,表示有多少组的多项式需要求和。(1 < n < 100)
下面为2n行整数,每一行都是一个多项式的表达式。表示n组需要相加的多项式。
每行长度小于300。
输出
输出包括n行,每行为1组多项式相加的结果。
在每一行的输出结果中,多项式的每一项用“[x y]”形式的字符串表示,x是该项的系数、y 是该项的幂数。要求按照每一项的幂从高到低排列,即先输出幂数高的项、再输出幂数低的项。
系数为零的项不要输出。`
2
-1 17 2 20 5 9 -7 7 10 4 22 2 -15 0 16 5 0 -1
2 19 7 7 3 17 4 4 15 10 -10 5 13 2 -7 0 8 -8
-1 17 2 23 22 2 6 8 -4 7 -18 0 1 5 21 4 0 -1
12 7 -7 5 3 17 23 4 15 10 -10 5 13 5 2 19 9 -7
[ 2 20 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 5 9 ] [ 6 5 ] [ 14 4 ] [ 35 2 ] [ -22 0 ]
[ 2 23 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 6 8 ] [ 8 7 ] [ -3 5 ] [ 44 4 ] [ 22 2 ] [ -18 0 ]
#include <iostream>
using namespace std;
typedef struct LNode
{
int coefficient,index;
struct LNode *next;
} LNode,*LinkList;
LNode * find(LNode *head,int b)
{
LNode*p=head->next;
while(p&&p->index!=b)
{
p=p->next;
}
return p;
}
void ListInsert(LNode*head,int a,int b)
{
LNode*p=new LNode;
p->coefficient=a;
p->index=b;
p->next=head->next;
head->next=p;
}
void sort(LNode*head)
{
LNode *p,*q;
int a;
for(p=head->next; p!=NULL; p=p->next)//冒泡排序。
for(q=head->next; q->next!=NULL; q=q->next)
{
if(q->index<q->next->index)
{
a=q->coefficient;
q->coefficient=q->next->coefficient;
q->next->coefficient=a;
a=q->index;
q->index=q->next->index;
q->next->index=a;
}
}
}
int main()
{
LNode*head=NULL,*p;
int t=2,a,b,n;
cin>>n;
while(n--)
{
head=new LNode;
head->next=NULL; //方便从头遍历。
t=2;
while(t--)
{
while(cin>>a>>b)
{
if(b<0)
break;
if((p=find(head,b))!=NULL) //查找如果指数相同,则系数相加。
{
p->coefficient+=a;
}
else
{
ListInsert(head,a,b);
}
}
}
sort(head);
p=head->next;
while(p!=NULL)
{
if(p->coefficient==0)
{
p=p->next;
continue;
}
cout<<"[ "<<p->coefficient<<" "<<p->index<<" ] ";
p=p->next;
}
cout<<endl;
}
return 0;
}
G++与GCC都是c++、c,但要用G++提交。