题109.pat甲级练习-1009 Product of Polynomials (25 分)
一、题目
二、题解
本题经典只考虑非零项,不然测试点0过不去
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<int,float,greater<int>> A,B,res;
int K;
cin>>K;
for(int i=0; i<K; i++)
{
int e;
float c;
cin>>e>>c;
A[e]=c;
}
cin>>K;
for(int i=0; i<K; i++)
{
int e;
float c;
cin>>e>>c;
B[e]=c;
}
map<int,float,greater<int>>::iterator it1,it2;
for(it1=A.begin(); it1!=A.end(); it1++)
{
for(it2=B.begin(); it2!=B.end(); it2++)
{
res[it1->first+it2->first]+=it1->second*it2->second;
}
}
int k=0;
map<int,float,greater<int>>::iterator it;
for(it=res.begin(); it!=res.end(); it++)//只考虑非零项
{
if(it->second!=0)
{
k++;
}
}
cout<<k<<" ";
int i=0;
for(it=res.begin(); it!=res.end(); it++,i++)//只考虑非零项
{
if(it->second!=0)
{
printf("%d %.1f",it->first,it->second);
if(i<k-1)
{
putchar(' ');
}
}
}
}