1481 多项式乘积 PAT
作者:
NikoNairre
,
2023-11-15 20:31:40
,
所有人可见
,
阅读 97
#include <iostream>
#include <iomanip>
using namespace std;
const int N = 2010;
double a[N], b[N], c[N];
int last = 2010;
int count_valid(double c[])
{
int cnt = 0;
for (int i = 2000; i >= 0; i-- ) {
if (c[i] != 0) {
cnt++ ;
last = i;
}
}
return cnt;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int k1; cin >> k1;
while (k1-- ) {
int pow; double val;
cin >> pow >> val;
a[pow] = val;
}
int k2; cin >> k2;
while (k2-- ) {
int pow; double val;
cin >> pow >> val;
b[pow] = val;
}
for (int i = 0; i <= 1000; i++ ) {
double cb = b[i];
for (int j = 0; j <= 1000; j++ ) {
double ca = a[j];
c[i + j] += ca * cb;
}
}
int cnt = count_valid(c);
cout << cnt << " ";
for (int i = 2000; i >= 0; i-- ) {
if (c[i] != 0) {
cout << i << " " << fixed << setprecision(1) << c[i];
if (i != last) cout << " ";
}
}
return 0;
}