题意:给两个表达式,算他们的积
#include<iostream>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
struct ss {
int exp;
double coef;
};
bool cmp(struct ss a,struct ss b) {
return a.exp>b.exp;
}
int main() {
int n,m;
cin>>n;
struct ss polyA[n];
for(int i=0; i<n; ++i)
cin>>polyA[i].exp>>polyA[i].coef;
cin>>m;
struct ss ans[n*m];
double exp,coef;
int k=0;
for(int i=0; i<m; ++i) {
cin>>exp>>coef;
for(int j=0; j<n; ++j) {//多项式运算
ans[k].exp=exp+polyA[j].exp;
ans[k++].coef=coef*polyA[j].coef;
}
}
sort(ans,ans+k,cmp);
queue<struct ss> s;
vector<struct ss> ans1;
s.push(ans[0]);
int flag=0;
for(int i=1; i<k; ++i) {
if(s.front().exp==ans[i].exp)//合并多项式
s.front().coef+=ans[i].coef;
else {
if(fabs(s.front().coef)>0.05)//坑:非零项才算数
ans1.push_back(s.front());
s.pop();
s.push(ans[i]);
}
}
if(!s.empty())
{
ans1.push_back(s.front());
s.pop();
}
cout<<ans1.size();
for(int i=0; i<ans1.size(); ++i)
printf(" %d %.1lf",ans1[i].exp,ans1[i].coef);
return 0;
}