#include<iostream>
#include<malloc.h>
#include<iomanip>
using namespace std;
typedef struct node{
int e;
double c;
struct node *next;
}link;
int main(){
int k1,k2,count,e1;
double c1;
cin>>k1;
count=k1;
link *h1=(link*)malloc(sizeof(link));//头结点
link *l1=h1;
for(int i=0;i<k1;i++){
cin>>e1>>c1;
if(c1!=0){
link *p=(link*)malloc(sizeof(link));
p->e=e1;
p->c=c1;
l1->next=p;
p->next=NULL;
l1=l1->next;
}
else
count--;
}
cin>>k2;
link *h2=(link*)malloc(sizeof(link));//头结点
link *l2=h2;
for(int i=0;i<k2;i++){
cin>>e1>>c1;
if(c1!=0){
link *p=(link*)malloc(sizeof(link));
p->e=e1;
p->c=c1;
l2->next=p;
p->next=NULL;
l2=l2->next;
}
}
l1=h1->next;
l2=h2->next;
link *pre1=h1;
link *pre2=h2;
while(l1!=NULL&&l2!=NULL){
if(l1->e==l2->e){
l1->c=l1->c+l2->c;
if(l1->c!=0){
pre1=l1;
l1=l1->next;
pre2->next=l2->next;
free(l2);
l2=pre2->next;
}
else{
pre1->next=l1->next;
free(l1);
l1=pre1->next;
pre2->next=l2->next;
free(l2);
l2=pre2->next;
count--;
}
}
else if(l1->e<l2->e){
pre2->next=l2->next;
pre1->next=l2;
pre1=l2;
l2->next=l1;
l2=pre2->next;
count++;
}
else{
pre1=l1;
l1=l1->next;
}
}
if(l2!=NULL){
pre1->next=l2;
}
while(l2!=NULL){
count++;
l2=l2->next;
}
free(h2);
l1=h1->next;
cout<<count;
while(l1!=NULL){
printf(" %d %.1lf",l1->e,l1->c);
l1=l1->next;
}
return 0;
}
/*这道题目学过,但是,给的测试案例太坑了,因为忘记保留一位小数,一直有几个测试点过不了。。。。*/
1002. A+B for Polynomials (25)
最新推荐文章于 2024-01-28 22:42:58 发布
