#include<stdio.h>
#include<stdlib.h>
typedef struct Node *Pet;
typedef Pet List;
struct Node{
int cef;
int exp;
Pet Next;
};
void Print(List p);
List read()
{
List p1,head,t;
int i,n,a,b;
scanf("%d",&n);
t=(Pet)malloc(sizeof(struct Node));
head=t;
for(i=0;i<n;i++){
scanf("%d %d",&a,&b);
p1=(Pet)malloc(sizeof(struct Node));
p1->cef=a;
p1->exp=b;
head->Next=p1;
head=p1;//
}
head->Next=NULL;
return t->Next;
}
List addition(List P1,List P2){
List temp,head,t,pa,pb;
pa=P1;
pb=P2;
head=(Pet)malloc(sizeof(struct Node));
head->Next=NULL;
t=head;
while(pa&&pb){
temp=(Pet)malloc(sizeof(struct Node));
if(pa->exp<pb->exp){
temp->cef=pb->cef;
temp->exp=pb->exp;
t->Next=temp;
t=temp;
pb=pb->Next;
}
else if(pa->exp==pb->exp){
temp->cef=pb->cef+pa->cef;
temp->exp=pb->exp;
t->Next=temp;
t=temp;
pb=pb->Next;
pa=pa->Next;
}
else if(pa->exp>pb->exp){
temp->cef=pa->cef;
temp->exp=pa->exp;
t->Next=temp;
t=temp;
pa=pa->Next;
}
}
if(pa)
t->Next = pa;
else if(pb)
t->Next = pb;
return head->Next;
}
void Print(List p){
List t=p;
t=p;
int flag=1;
for(;t;t=t->Next){
if(t->cef){
printf("%d %d",t->cef,t->exp);
flag=0;}
if(!flag&&t->cef){
printf(" ");
}}
if(flag)
printf("0 0");
printf("\n");
}
List multiplication(List p1,List p2){
List a,b,mul,temp,head;
a=p1;
b=p2;
mul=(List)malloc(sizeof(struct Node));
mul->Next=NULL;
head=mul;
for(;a;a=a->Next){
for(b=p2;b;b=b->Next){
temp=(List)malloc(sizeof(struct Node));
temp->cef=(a->cef)*(b->cef);
temp->exp=(a->exp)+(b->exp);
temp->Next=NULL;
head=addition(temp,mul);
mul=head;
}
}
return head;//
}
int main(){
List L1,L2,ad,mul;
L1 = read();
L2 = read();
ad = addition(L1,L2);
mul = multiplication(L1,L2);
Print(mul);
Print(ad);
return 0;
}
这个输出的结果不知道为什么会有一个很大的数字和0