1002 A+B for Polynomials (25 point(s))

1002 A+B for Polynomials (25 point(s))

Just define an array ans[ ] ans[ ]a n s [ ] , add its coefficient at the position of the corresponding power. When looking at how many items there are in total, count according to whether the coefficient is 0. Then output again from high to low

#include <stdio.h>
#include <iostream>
using namespace std;
const int maxn = 1e6+10;


double ans[maxn];
int main(){
    
    

    int k;cin>>k;
    for(int i = 1;i<=k;i++){
    
    
        int a;double b;scanf("%d %lf",&a,&b);
        ans[a] += b;
    }
    cin>>k;
    for(int i = 1;i<=k;i++){
    
    
        int a;double b;scanf("%d %lf",&a,&b);
        ans[a] += b;
    }

    int cnt = 0;
    for(int i = 0;i<=1000;i++) if(ans[i] != 0) cnt++;
    cout<<cnt;
    for(int i = 1000;i>=0;i--){
    
    
        if(ans[i] != 0) {
    
    
            printf(" %d %.1f",i,ans[i]);
        }
    }


    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326229371&siteId=291194637