PAT甲级 1009 Product of Polynomials (25 分) 题解

PAT甲级 1009 Product of Polynomials (25 分) 题解_第1张图片PAT甲级 1009 Product of Polynomials (25 分) 题解_第2张图片
\quad 模拟多项式乘法,很简单,程序如下:

#include 
using namespace std;

int main()
{
	float coef[2001]={0}, a[1001]={0}, b[1001]={0};
	int num1, num2;
	int exp;
	float temp;
	scanf("%d", &num1);
	for (int i = 0; i < num1; ++i)
	{
		scanf("%d%f", &exp, &temp);
		a[exp] = temp;
	}
	scanf("%d", &num2);
	for (int i = 0; i < num2; ++i)
	{
		scanf("%d%f", &exp, &temp);
		b[exp] = temp;
	}
	for (int i = 0; i < 1001; ++i)
	{
		for (int j = 0; j < 1001; ++j)
		{
			coef[i+j] += a[i]*b[j];
		}
	}
	int cnt = 0;
	for (int i = 0; i < 2001; ++i)
	{
		if(coef[i]!=0.0) cnt++;
	}
	printf("%d", cnt);
	for (int i = 2001; i >= 0; --i)
	{
		if(coef[i]!=0.0) printf(" %d %.1f", i, coef[i]);
	}
	return 0;
}

你可能感兴趣的:(PAT甲级)