pat 1009 Product of Polynomials

简单的多项式相乘。

#include<iostream>
using namespace std;
const int iMax=5005;
double a[iMax]={0},b[iMax]={0},c[iMax]={0};
int main()
{
	int n,m,i,j,alen,blen,index,count;
	double val;
	//freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin);
	scanf("%d",&n);
	alen=blen=-1;
	for(i=0;i<n;i++){
		scanf("%d%lf",&index,&val);
		a[index]=val;
		if(alen==-1)
			alen=index;
	}
	scanf("%d",&m);
	for(i=0;i<m;i++){
		scanf("%d%lf",&index,&val);
		b[index]=val;
		if(blen==-1)
			blen=index;
	}
	for(i=0;i<=alen;i++){
		for(j=0;j<=blen;j++){
			c[i+j]+=a[i]*b[j];
		}
	}
	count=0;
	for(i=alen+blen;i>=0;i--)
		if(c[i]!=0)
			count++;
	printf("%d",count);
	for(i=alen+blen;i>=0;i--)
		if(c[i]!=0)
			printf(" %d %.1lf",i,c[i]);
	return 0;
}


 

你可能感兴趣的:(pat,pat)