PAT 1009
水题,注意乘积系数为0的项不要输出。
另外用G++编译有问题,用clang就没问题,还没找到原因,猜测可能是因为map中初始化的问题。
代码:
#include<iostream>
#include<map>
#include<algorithm>
#include<iomanip>
using namespace std;
int K = 0;
struct cmp{
bool operator()(const int& o1, const int& o2)const{
return o1 > o2;
}
};
int main(){
map<int,double> m1,m2;
map<int,double,cmp> res;
cin>>K;
for(int i = 0;i < K;i++){
int tmp;
cin>>tmp>>m1[tmp];
}
cin>>K;
for(int i = 0;i < K;i++){
int tmp;
cin>>tmp>>m2[tmp];
}
for(map<int,double>::iterator it1 = m1.begin();it1 != m1.end();it1++)
for(map<int,double>::iterator it2 = m2.begin();it2 != m2.end();it2++){
if(!res.count(it1->first+it2->first)) res[it1->first+it2->first] = 0;
res[it1->first+it2->first] += it1->second * it2->second;
}
int s = (int) res.size();
for(map<int,double>::iterator ite = res.begin();ite != res.end();ite++){
if(ite->second == 0) s--;
}
cout<<s<<" ";
int i = 0;
map<int,double,cmp>::iterator ite = res.begin();
for(;i < res.size()-1;ite++,i++){
if(ite->second != 0)
cout<<ite->first<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<ite->second<<" ";
}
if(ite->second != 0)
cout<<ite->first<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<ite->second;
}
6ms