This time, you are supposed to find A×B where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K N1 aN1 N2 aN2 ... NK aNK
where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤NK<⋯<N2<N1≤1000.
Output Specification:
For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.
Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 3 3.6 2 6.0 1 1.6
#include<iostream>
#include<iomanip>
#include<map> //有序,互异性
#define rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
int main()
{
map<int,float> poly_a,poly_b,poly;
int k1,k2,cnt=0;
cin>>k1;
rep(i,1,k1){
int n;
float a;
cin>>n>>a;
poly_a[n]=a;
}
cin>>k2;
rep(i,1,k2){
int n;
float a;
cin>>n>>a;
poly_b[n]+=a;
}
for(auto it1:poly_a){
for(auto it2:poly_b){
poly[it1.first+it2.first]+=it1.second*it2.second;
}
}
for(auto it:poly){
if(it.second) cnt++;
} /*有的项可能乘完之后系数会变成0,poly.size()是所有变量的大小
使用size()则第一个测试点无法通过,需遍历poly记录系数不为0的变量个数*/
cout<<cnt;
for(auto it=poly.rbegin();it!=poly.rend();++it){
if(it->second) cout<<' '<<it->first<<' '<<fixed<<
setprecision(1)<<it->second;
}
cout<<'\n';
return 0;
}
/*rbegin() 返回一个反向迭代器,它指向数组的最后一个元素,
rend() 返回一个反向迭代器,它指向数组的第一个元素之前的位置
*/
第一个测试点答案含有为0的项,需要确保该项不被输出