PAT 1002. A+B for Polynomials (25)

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

这个题我能说我交了无数次吗? 坑, 系数是0 不能输出

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<map>
 4 
 5 using namespace std;
 6 
 7 const double eps = 1e-9;
 8 int main() {
 9         int k;
10         while(cin >> k) {
11                 map<int, double> p;
12                 int e; 
13                 double a;
14                 for (int i = 0; i < k; ++i) {
15                         cin >> e >> a;
16                         p[e] = a;
17                 }
18                 cin >> k;
19                 for (int i = 0; i < k; ++i) {
20                         cin >> e >> a;
21                         if (p.count(e)) {
22                                 double x = p[e];
23                                 if (!(x + a > -eps && x + a < eps)) {
24                                         p[e] += a;
25                                 } else {
26                                         p.erase(e);
27                                 }
28                         } else {
29                                 p[e] = a;
30                         }
31                 }
32                 cout << p.size();
33                 for (map<int, double>::reverse_iterator iter = p.rbegin(); iter != p.rend(); ++iter) {
34                         cout << " " << iter->first << " ";
35                         cout << fixed << setprecision(1) << iter->second; 
36                 }
37         }
38         return 0;
39 }

 

posted @ 2015-04-13 23:14  ACSeed  Views(366)  Comments(1)    收藏  举报