Title description
For this question, the meaning of the question is very simple. It is to add two polynomials. After the output coefficients and values,
I use map to add the polynomials, but I don’t know where the problem lies . The idea is exactly the same as that of the big guy, but the score is only After getting a part of the score ( caicaicai ), it turns out that the coefficient may be 0 without consideration.
Which uses reverse traversal map
#include<iostream>
#include<map>
#include<cstdio>
using namespace std;
map<int,double>a;
int main(){
int num;
int x;
double y;
cin>>num;
for(int i=0;i<num;i++){
cin>>x>>y;
a[x]+=y;
}
cin>>num;
for(int i=0;i<num;i++){
cin>>x>>y;
a[x]+=y;
if(a[x]==0)
a.erase(x);
}
cout<<a.size();
map<int, double>::reverse_iterator iter;
for(iter = a.rbegin(); iter != a.rend(); iter++){
//if(iter->second!=0)
printf(" %d %.1f",iter->first, iter->second );
}
}
The big guy uses an array solution (the idea is the same, why is mine wrong)
#include<bits/stdc++.h>
using namespace std;
double coefficient[1005]={
0};
int main(){
int K;
scanf("%d",&K);
while(K--){
int a;
double b;
scanf("%d%lf",&a,&b);
coefficient[a]+=b;
}
scanf("%d",&K);
while(K--){
int a;
double b;
scanf("%d%lf",&a,&b);
coefficient[a]+=b;
}
int size=0;
for(int i=0;i<1005;++i)
if(coefficient[i]!=0)
++size;
printf("%d",size);
for(int i=1005;i>=0;--i)
if(coefficient[i]!=0)
printf(" %d %.1f",i,coefficient[i]);
return 0;
}