注意K是系数非0项的个数,因此a+b=0时,只需输出0即可;而在有些题目中,会要求输出项的个数,则结果为1
#include <bits/stdc++.h>
using namespace std;
int main()
{
// ios::sync_with_stdio(false);
double c[1001];
memset(c, 0, sizeof(c));
int count = 0;
for (int i = 0; i < 2; i++)
{
int n;
cin >> n;
int a;
double b;
for (int j = 0; j < n; j++)
{
cin >> a >> b;
if (c[a] == 0)
count++;
c[a] += b;
if (c[a] == 0)
count--;
}
}
cout << count;
for (int i = 1000; i >= 0; i--)
{
if (c[i] != 0)
printf(" %d %.1f", i, c[i]);
}
}