PAT 1009. Product of Polynomials (25)

题目地址: http://www.patest.cn/contests/pat-a-practise/1009

#include<iostream>
#include<iomanip>
#include<cstring>
#include<map>
#include<vector>

using namespace std;

const int MAXN  = 1000 + 10;
const double eps = 1e-9; 
struct Item {
        int e;
        double a;
        Item(int _e, double _a) {
                e = _e;
                a = _a;
        }
};
int main() {
        int k;
        while(cin >> k) {
                int e;
                double a;
                vector<Item> p;
                for (int i = 0; i < k; ++i) {
                        cin >> e >> a;
                        Item item(e, a);
                        p.push_back(item);
                }
                map<int, double> ans;
                cin >> k;
                for (int i = 0; i < k; ++i) {
                        cin >> e >> a;
                        for (int j = 0; j < p.size(); ++j) {
                                int ans_e = p[j].e + e;
                                double ans_a= p[j].a * a;
                                if (ans.count(ans_e)) {
                                        double x = ans[ans_e];
                                        if ((x + ans_a > -eps && x + ans_a < eps)) {
                                                ans.erase(ans_e);
                                        } else {
                                                ans[ans_e] += ans_a;
                                        }
                                } else {
                                        ans[ans_e] = ans_a;
                                }
                        }
                }
                cout << ans.size();
                for (map<int, double> :: reverse_iterator iter = ans.rbegin(); iter != ans.rend(); ++iter) {
                        cout << " " << iter->first << " ";
                        cout << fixed << setprecision(1)<< iter->second;
                }
                cout << endl;
        }
        return 0;
}

 

posted @ 2015-04-14 13:48  ACSeed  Views(134)  Comments(0)    收藏  举报