pat甲级打卡-1009 Product of Polynomials

1002的进阶题

#include<bits/stdc++.h>
using namespace std;


int k1,k2;
map<float,float> m,n;
int main(){
    cin>>k1;
    int cnt=0;
    while(k1--){
        float key,val;
        cin>>key>>val;
        if(!m.count(key))  m[key]=val;
        else m[key]+=val;
    }
    
    cin>>k2;
    while(k2--){
        float key,val;
        cin>>key>>val;
        for(auto it:m){
            float newKey=key+it.first;
            float newVal=val*it.second;
           
            if(!n.count(newKey)) n[newKey]=newVal;
            else n[newKey]+=newVal;
            if(n[newKey]==0) n.erase(newKey);
        }  
    }
    
    for(auto it:n){
        if(it.second!=0) cnt++;
    }
    cout<<cnt<<" ";
    for(auto it=n.rbegin();it!=n.rend();it++){
        if(it!=n.rbegin()) cout<<" ";
        //cout<<it->first<<" "<<it->second;
        printf("%.0f %.1f",it->first,it->second);
    }
    return 0;
}

posted @ 2022-04-29 23:06  秋月桐  阅读(12)  评论(0编辑  收藏  举报