A1002
#include<iostream>
using namespace std;
const int maxn=1010;
double a[maxn],b[maxn],c[maxn];
int main()
{
int k;
cin >> k;
while(k--)
{
int n;
double v;
cin >> n >> v;
a[n]=v;
}
cin >> k;
while(k--)
{
int n;
double v;
cin >> n >> v;
b[n]=v;
}
for(int i=0; i<maxn; i++)
c[i]=a[i]+b[i];
int kk=0;
for(int i=0; i<maxn; i++)
if(c[i])
kk++;
cout << kk;
for(int i=maxn-1; i>=0; i--)
if(c[i])
printf(" %d %.1lf",i,c[i]);
return 0;
}
A1009
#include <iostream>
using namespace std;
const int N = 1010;
double a[N], b[N], c[N * 2];
void input(double a[])
{
int k;
cin >> k;
while (k -- )
{
int n;
double v;
cin >> n >> v;
a[n] = v;
}
}
int main()
{
input(a);
input(b);
// 做乘法
for (int i = 0; i < N; i ++ ) //i去遍历b,j去遍历a
for (int j = 0; j < N; j ++ )
c[i + j] += b[i] * a[j]; //i+j为次数,b[i]*a[j]为系数
int k = 0;
for (int i = 0; i < N * 2; i ++ )
if (c[i])
k ++ ;
cout << k;
for (int i = N * 2 - 1; i >= 0; i -- )
if (c[i])
printf(" %d %.1lf", i, c[i]);
return 0;
}