天天看点

PAT甲级 1009. Product of Polynomials (25分)

代码参考胡凡笔记 

#pragma warning(disable:4996)
#include<cstdio>
#include<vector>
using namespace std;

/*
	总结:这道题关键在于多项式乘积的理解
	多项式相乘的结果,指数是两者指数相加,系数相乘
	注意点:
	1)答案的系数数组要够大,起码大于2000
	2)第二个数组不用保存,可以边读边边处理
*/


struct Poly {
	int exp;
	double value;
}poly[1001];

double ans[2001];

int main() {
	int n, m, number = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d %lf", &poly[i].exp, &poly[i].value);
	}
	scanf("%d", &m);
	for (int i = 0; i < m; i++) {
		int exp;
		double value;
		scanf("%d %lf", &exp, &value);
		for (int j = 0; j < n; j++) {
			ans[exp + poly[j].exp] += (value*poly[j].value);
		}
	}
	for (int i = 0; i <= 2000; i++) {
		if (ans[i] != 0.0)number++;
	}
	printf("%d", number);
	for (int i = 2000; i >= 0; i--) {
		if (ans[i] != 0.0) {
			printf(" %d %.1f", i, ans[i]);
		}
	}
	system("pause");
	return 0;
}