1002 A+B for Polynomials (25 分)
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
int maxn = 1010;
double a[maxn];
memset(a,0,sizeof(double)*maxn);
cin>>n;
int exponent;
double coef;
while (n--) {
cin>>exponent>>coef;
a[exponent] += coef;
}
cin>>m;
int count = 0;//count记载不为0指数个数
while (m--) {
cin>>exponent>>coef;
a[exponent] += coef;//相同指数的,系数相加
}
for (int i = 0; i < maxn; i++) {
if (a[i] != 0) {//系数为0的丢弃
count++;
}
}
cout<<count;
for (int i = maxn; i >= 0; i--) {
if (a[i] != 0) {
printf(" %d %.1f", i, a[i]);
}
}
return 0;
}