题意:就是将两个多项式加起来再输出。

题目虽然简单但是坑很多。当两个多项式想加抵消为0时,只要输出0即可。

英文题,一开始有点没读懂题目,没看见它最后的结果要保留一位小数。

 1 #include<iostream>
 2 #include<vector>
 3 #include<map>
 4 #include<cmath>
 5 #include<cstdio>
 6 #include<cstdlib>
 7 #include<cstring>
 8 #include<algorithm>
 9 using namespace std;
10 
11 const int maxSize = 1000 + 10;
12 double P[maxSize];
13 
14 
15 int main()
16 {
17     int K, index;
18     double  val;
19     memset(P, 0, sizeof(P));
20     map<int, double> map;
21     for (int i = 0; i < 2; i++) {
22         cin >> K;
23         while (K--) {
24             cin >> index >> val;
25             P[index] += val;
26             if (abs(P[index])<1e-10) { //等于0,如果map中存在过去记录的index,则移除
27                 auto iter = map.find(index);
28                 if (iter != map.end()) map.erase(iter); //将过去记录的index删除
29             }
30             else { //不等于0,则记录下来
31                 map[index] = P[index];
32             }
33         }
34     }
35     cout << map.size();
36     if (map.size() != 0) cout << " ";
37     int i = 1;
38     for (auto iter = map.rbegin(); iter != map.rend(); iter++) {
39         printf("%d %.1lf", iter->first, iter->second);
40         if (i++ != map.size()) cout << " ";
41     }
42     cout << endl;
43     return 0;
44 }
posted on 2019-12-24 23:01  Chenjin123  阅读(335)  评论(0编辑  收藏  举报