输入格式:输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0
input sample:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
output sample:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
#include<stdio.h>
#include<stdlib.h>
//乘法没有清空0只是输出没有0 加法是清空0的
struct arrnode{
int base;
int index;
};
struct listnode{
int base;
int index;
struct listnode *next;
};
int main()
{
//0没有考虑
int a,b,i,j,tpbase,tpindex,flag1;
scanf("%d",&a);
if(a==0)
{
scanf("%d",&b);
if(b==0)
{
printf("0 0\n0 0");
}
else{
struct arrnode arr2[b];
for(i=0;i<b;i++)
scanf("%d %d",&arr2[i].base,&arr2[i].index);
printf("0 0\n");
for(i=0;i<b-1;i++)
printf("%d %d ",arr2[i].base,arr2[i].index);
printf("%d %d",arr2[i].base,arr2[i].index);
}
return 0;
}
struct arrnode arr1[a];
for(i=0;i<a;i++)
scanf("%d %d",&arr1[i].base,&arr1[i].index);
scanf("%d",&b);
if(b==0)
{
printf("0 0\n");
for(i=0;i<a-1;i++)
printf("%d %d ",arr1[i].base,arr1[i].index);
printf("%d %d",arr1[i].base,arr1[i].index);
return 0;
}
struct arrnode arr2[b];
for(i=0;i<b;i++)
scanf("%d %d",&arr2[i].base,&arr2[i].index);
struct listnode *mul,*add,*tempmul,*tempadd,*temp;
mul=(struct listnode*)malloc(sizeof(struct listnode));
add=(struct listnode*)malloc(sizeof(struct listnode)); //这两个是结果的头结点
tempadd=add;
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
tempmul=mul;
tpbase=arr1[i].base*arr2[j].base;
tpindex=arr1[i].index+arr2[j].index;
while(tempmul->next)
{
if(tempmul->next->index==tpindex)
{
tempmul->next->base+=tpbase;
break;
}
if(tempmul->next->index>tpindex)
{
tempmul=tempmul->next;
continue;
}
if(tempmul->next->index<tpindex)
{
temp=(struct listnode*)malloc(sizeof(struct listnode));
temp->base=tpbase;
temp->index=tpindex;
temp->next=tempmul->next;
tempmul->next=temp;
break;
}
}
if(tempmul->next==NULL)
{
temp=(struct listnode*)malloc(sizeof(struct listnode));
temp->base=tpbase;
temp->index=tpindex;
tempmul->next=temp;
}
}
}
while(mul->next->next)
{
temp=mul->next;
if(temp->base!=0)
{
flag1=1;
printf("%d %d ",temp->base,temp->index);
}
mul=mul->next;
}
if(mul->next->base!=0)
{
flag1=1;
printf("%d %d\n",mul->next->base,mul->next->index);
}
else
printf("\n");
if(flag1==0)
printf("0 0\n");
//加法
i=0;j=0;
while(i<a&&j<b)
{
temp=(struct listnode*)malloc(sizeof(struct listnode));
if(arr1[i].index>arr2[j].index)
{
temp->index=arr1[i].index;
temp->base=arr1[i].base;
tempadd->next=temp;
tempadd=tempadd->next;
i++;
}
else if(arr1[i].index<arr2[j].index)
{
temp->index=arr2[j].index;
temp->base=arr2[j].base;
tempadd->next=temp;
tempadd=tempadd->next;
j++;
}
else{
temp->index=arr1[i].index;
temp->base=arr2[j].base+arr1[i].base;
if(temp->base!=0)
{
tempadd->next=temp;
tempadd=tempadd->next;
}
i++;j++;
}
}
while(i<a)
{
struct listnode *temp=(struct listnode*)malloc(sizeof(struct listnode));
temp->index=arr1[i].index;
temp->base=arr1[i].base;
tempadd->next=temp;
tempadd=tempadd->next;
i++;
}
while(j<b)
{
struct listnode *temp=(struct listnode*)malloc(sizeof(struct listnode));
temp->index=arr2[j].index;
temp->base=arr2[j].base;
tempadd->next=temp;
tempadd=tempadd->next;
j++;
}
if(add->next==NULL)
{
printf("0 0");
return 0;
}
while(add->next->next)
{
temp=add->next;
printf("%d %d ",temp->base,temp->index);
add=add->next;
}
printf("%d %d",add->next->base,add->next->index);
}