1009 Product of Polynomials (25 point(s))
This time, you are supposed to find A×B where A and B are two polynomials.
Example:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
struct Item {
int exp;
double co;
};
void input(vector<Item> &N)
{
Item item;
int K;
cin >> K;
for(int i = 0; i < K; i++) {
cin >> item.exp >> item.co;
N.push_back(item);
}
}
int main()
{
int K1, K2;
vector<Item> N1;
vector<Item> N2;
map<int, double> N3;
input(N1);
input(N2);
for(auto &x : N1) {
for(auto &y : N2) {
int exp = x.exp + y.exp;
N3[exp] += x.co * y.co;
}
}
int count = 0;
for(auto &x : N3)
if(fabs(x.second) >= 0.1)
count++;
cout << count;
for(auto x = N3.rbegin(); x != N3.rend(); x++)
if(fabs(x->second) >= 0.1)
printf(" %d %.1f", x->first, x->second);
cout << endl;
}
思路:
利用 stl/map 是红黑树实现的原理,保存并更新对应 exp 的系数,最后统计系数绝对值 >=0.1 的项数,输出