天天看點

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;
}