PAT甲级 1002 A+B for Polynomials (25)

#include <iostream>

double res[1001]={0};
using namespace std;
int main(int argc, const char * argv[]) {
    // insert code here...
    int k1,k2;
    cin>>k1;
    for (int i=0; i<k1; i++) {
        int exp;
        double cof;
        scanf("%d %lf",&exp,&cof);
        res[exp]+=cof;
    }
    cin>>k2;
    for (int i=0; i<k2; i++) {
        int exp;
        double cof;
        scanf("%d %lf",&exp,&cof);
        res[exp] += cof;
    }
    int res_k=0;
    for (int i=0; i<1001; i++) {
        if (res[i]!=0) {
            res_k++;
        }
    }
    cout<<res_k;
    for (int i=1000; i>=0; i--) {
        if (res[i]!=0) {
            printf(" %d %.1lf",i,res[i]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ken_for_learning/article/details/78311021