#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<utility>
#include<algorithm>
#include<iomanip>
using namespace std;
class Pol
{
public :
map<int,double> coe;
Pol operator*(const Pol& pol_b)
{
Pol pol_c;
map<int,double>::const_iterator it_a=coe.begin();
while(it_a!=coe.end())
{
map<int,double>::const_iterator it_b=pol_b.coe.begin();
while(it_b!=pol_b.coe.end())
{
(pol_c.coe)[it_a->first+it_b->first]+=it_a->second*it_b->second;
it_b++;
}
it_a++;
}
return pol_c;
}
void add(int key,double value)
{
coe[key]+=value;
}
};
ostream& operator<<(ostream& os,const Pol& pol)
{
cout.setf(ios::fixed);
cout.precision(1);
int sum=0;
map<int,double>::const_reverse_iterator rev_it=pol.coe.rbegin();
while(rev_it!=pol.coe.rend())
{
if(rev_it->second!=0)sum++;
rev_it++;
}
cout<<sum;
rev_it=pol.coe.rbegin();
while(rev_it!=pol.coe.rend())
{
if(rev_it->second!=0)
{
cout<<" "<<rev_it->first<<" "<<rev_it->second;
}
rev_it++;
}
return cout;
}
int main()
{
int len_a;
int len_b;
Pol pol_a;
Pol pol_b;
cin>>len_a;
for(int i=0;i<len_a;i++)
{
int key;
double val;
cin>>key>>val;
pol_a.add(key,val);
}
cin>>len_b;
for(int i=0;i<len_b;i++)
{
int key;
double val;
cin>>key>>val;
pol_b.add(key,val);
}
Pol pol_c=pol_a*pol_b;
cout<<pol_c;
}
ps:这题主要回顾一下C++风格写法以及map的应用。
(PAT)1009. Product of Polynomials (25)
最新推荐文章于 2022-02-25 19:38:17 发布