#include <iostream>
//稀疏多项式合并
typedef struct PNode
{int expn;//指数float coef;//系数struct PNode *next;
}PNode,*Polyn;
void create_polyn(Polyn &s,int n)
{s=new PNode ;//头节点s->next=nullptr;PNode *q=new PNode;PNode *pre=new PNode;q=s;for (int i = 0; i < n; ++i) {PNode *e=new PNode; //局部变量保存新节点std::cin>>e->coef>>e->expn;pre=s;//q的前驱q=s->next;//每次选位置前,q都要指向首元结点while (q&&q->expn<e->expn){pre=q;q=q->next;}//插入ee->next=q;pre->next=e;}
}
void Add_Polyn(Polyn &a,Polyn &b)
{
//相同指数,对应系数加,不同指数,小的先放,合并到aPNode *ap=new PNode ;PNode *bp=new PNode ;PNode *apre=new PNode ;PNode *bpre=new PNode ;ap=a->next,bp=b->next;apre=a,bpre=b;float sum;while (ap&&bp){PNode *node;if(ap->expn==bp->expn){//指数相等,系数相加不等于0和等于0sum = ap->coef + bp->coef;if (sum!=0) {ap->coef = sum;node=bp;bpre->next=bp->next;bp = bp->next;ap = ap->next;apre = apre->next;delete node;} else {node=ap;apre->next = ap->next;ap = ap->next;delete node;bpre->next = bp->next;node=bp;bp = bp->next;delete node;}}else if(ap->expn>bp->expn){bp->next=ap;apre->next=bp;apre=bp;bpre=bp;bp=bp->next;} else{apre=ap;ap=ap->next;}}
//剩下的while (bp){ap->next=bp;bp=bp->next;}
}
void output(Polyn a)
{a=a->next;while (a){printf("%0.2f %d ",a->coef,a->expn);a=a->next;}printf("\n");}
int main() {//两个稀疏多项式a,bPolyn a,b;int a_len,b_len;std::cin>>a_len;std::cin>>b_len;create_polyn(a,a_len);create_polyn(b,b_len);output(a);output(b);Add_Polyn(a,b);output(a);return 0;
}
input
c*x^y 输入系数c,指数y
4 3
3 0 2 2 -5 1 7 3
5 0 10 1 -2 2