PAT (Advanced Level) 1002. A+B for Polynomials (25)

时间:2023-03-10 08:47:30
PAT (Advanced Level) 1002. A+B for Polynomials (25)

为0的不要输出。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std; double a[],b[];
int k; int main()
{
for(int i=;i<=;i++) a[i]=b[i]=;
int Max=-;
scanf("%d",&k);
for(int i=;i<=k;i++)
{
int id;double num;
scanf("%d%lf",&id,&num);
Max=max(Max,id);
a[id]=num;
} scanf("%d",&k);
for(int i=;i<=k;i++)
{
int id;double num;
scanf("%d%lf",&id,&num);
Max=max(Max,id);
b[id]=num;
} int cnt=;
for(int i=Max;i>=;i--)
if(a[i]+b[i]!=) cnt++; printf("%d",cnt);
int op=;
for(int i=Max;i>=;i--)
{
if(a[i]+b[i]!=){
printf(" %d %.1lf",i,a[i]+b[i]);
}
}
return ;
}