PAT1009题解

#include<vector>
#include<map>
#include<iostream>
using namespace std;
typedef pair<int,double> P;
int cnt = 0; 
int main () {
	vector<P>A;
	vector<P>B;
	map<int,double>res;
	int n, exp, test = 2;
	double coff;
	cin >> n;
	for(int i = 0 ; i < n;i ++) {
		cin >> exp >> coff;
		A.push_back(P(exp, coff)); 
	}
	cin >> n;
	for(int i = 0 ; i < n;i ++) {
		cin >> exp >> coff;
		B.push_back(P(exp, coff)); 
	}
	for(int i = 0;i < A.size(); i++) {
		for(int j = 0;j < B.size();j++) {
			int cur_exp;
			if(A[i].first == 0) cur_exp = B[j].first;
			else if(B[i].first == 0) cur_exp = A[i].first;
			else cur_exp =  A[i].first + B[j].first;
			double cur_cof = A[i].second * B[j].second;
			if(res.find(cur_exp) != res.end()) cnt++;
			res[cur_exp] += cur_cof;
			if(res[cur_exp] == 0)  {
				cnt--;
				res.erase(cur_exp);
			}
		}
	}
 	cout << res.size() << " ";
	for(map<int,double>::reverse_iterator it = res.rbegin(); it != res.rend(); it++) {
		map<int,double>::reverse_iterator temp = it;
		temp++;
		if(temp != res.rend())  printf("%d %.1f ",it->first, it->second);
		else  printf("%d %.1f",it->first, it->second);
	}
} 

  统计 多项式个数的时候 一定要注意两次判断。第一次判断这个项是否存在,第二次判断这个项的系数在经过各种操作之后是否会变成0。

posted @ 2019-02-01 11:03  奥利奥王子饼干  阅读(155)  评论(0编辑  收藏  举报