PAT(Advance)1009. Product of Polynomials
该题分制为25分。
题目链接https://pintia.cn/problem-sets/994805342720868352/problems/994805509540921344
题目要求模拟两个多项式相乘,那A*B,一个个乘呗。结果我用map来存不用考虑开多大的数组了。最后vale为0的不输出。
#include<iostream>
#include<map>
using namespace std;
struct polynomials
{
int e;
double c;
};
map<int, double> res;
int main()
{
polynomials p1[11], p2[11];
int k1 = 0;
cin >> k1;
for (int j = 0; j < k1; j++)
{
cin >> p1[j].e >> p1[j].c;
}
int k2 = 0;
cin >> k2;
for (int j = 0; j < k2; j++)
{
cin >> p2[j].e >> p2[j].c;
}
for (int i = 0; i < k1; i++)
{
for (int j = 0; j < k2; j++)
{
int index = p1[i].e + p2[j].e;
res[index] += p1[i].c * p2[j].c;
}
}
int sum = 0;
for (map<int, double>::reverse_iterator it = res.rbegin(); it != res.rend(); it++)
{
if (it->second != 0.0)
sum++;
}
cout << sum;
for (map<int, double>::reverse_iterator it = res.rbegin(); it != res.rend(); it++)
{
if (it->second != 0.0)
printf(" %d %.1f", it->first, it->second);
}
return 0;
}