-
Notifications
You must be signed in to change notification settings - Fork 1
/
q2.cpp
48 lines (39 loc) · 813 Bytes
/
q2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <map>
#include <iostream>
#include <functional>
using namespace std;
// 2. 多项式加法
int main() {
int n;
cin >> n;
while (n--) {
map <int, int, greater<int> > poly;
// 读入多项式,第二次读入时求和
int i = 2;
while (i--) {
while (1) {
int degree = 0;
int coefficient = 0;
cin >> coefficient;
cin >> degree;
if (degree < 0) {
break;
}
if (coefficient == 0) {
continue;
}
if (poly.find(degree) != poly.end()) {
poly[degree] += coefficient;
} else {
poly.insert(make_pair(degree, coefficient));
}
}
}
for (map<int, int>::iterator it = poly.begin(); it != poly.end(); it++) {
if (it->second != 0) {
cout << "[ " << it->second << ' ' << it->first << " ] ";
}
}
cout << endl;
}
}