PAT甲级1009 测试点0

1009 Product of Polynomials (25分)

  • 测试点0:系数项可能为负数,所以有些项的系数会为零,此时不应输出
#include
using namespace std;

int main(){
	int n;
	cin>>n;
	map<int ,float> a;
	for(int i=0;i<n;i++){
		int tmp1;float tmp2;
		cin>>tmp1>>tmp2;
		a[tmp1]=tmp2;
	}
	cin>>n;
	map<int ,float> b;
	for(int i=0;i<n;i++){
		int tmp1;float tmp2;
		cin>>tmp1>>tmp2;
		b[tmp1]=tmp2;
	}
	
	map<int, float,greater<int>> ans;
	map<int,float> :: iterator iter1=a.begin();
	while(iter1!=a.end()){
		map<int,float> :: iterator iter2=b.begin();
		while(iter2!=b.end()){
			int tmp1=iter1->first+iter2->first;
			float tmp2=iter1->second*iter2->second;
			if(ans.find(tmp1)==ans.end())ans[tmp1]=tmp2;
			else ans[tmp1]+=tmp2;
      if(ans[tmp1]==0)ans.erase(tmp1);
			iter2++;
		}
		iter1++;
	}
	
	map<int,float> :: iterator iter=ans.begin();
	cout<<ans.size();
	while(iter!=ans.end()){
		cout<<" "<<iter->first<<" ";
		printf("%.1f",iter->second);
		iter++;
	}
	return 0;
}

你可能感兴趣的:(PAT甲级刷题历程)