天天看點

1009 Product of Polynomials (25 分)

題目連結:https://pintia.cn/problem-sets/994805342720868352/problems/994805509540921344

AC代碼:關于測試點1,3

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxn = 1010;
float A[maxn],B[maxn],res[maxn+maxn];
//底數值一律換成float型存儲,否則,測試點1,3通不過
int main() {
	int n1,n2;
	cin >> n1;
	for (int i = 0; i < n1; i++) {
		int exp;
		float cof;
		cin >> exp>>cof;
		A[exp] = cof;
	}
	cin >> n2;
	for (int i = 0; i < n2; i++) {
		int exp;
		float cof;
		cin >> exp >> cof;
		B[exp] = cof;
	}
	for (int i = 0; i < maxn; i++) {
		if (A[i] != 0) {
			for (int j = 0; j < maxn; j++) {
				if (B[j] != 0) {
					res[i + j] += A[i] * B[j];
				}
			}
		}
	}
	int cnt = 0;
	float t = 0.05;
	for (int i = 0; i < maxn * 2; i++) {
		if (abs(res[i])>=t) {
			cnt++;
		}
	}
	printf("%d",cnt);
	for (int i = maxn*2; i >=0; i--) {
		if (abs(res[i]) >= t) {
			if (cnt) {
				printf(" ");
				cnt -= 1;
			}
			printf("%d %.1f",i,res[i]);
		}
	}
	return 0;
}