1002. A+B for Polynomials (25)


#include <iostream>
#include <algorithm>
using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    int k;
    double a[1001];
    fill(a, a + 1001, 0);
    scanf("%d", &k);
    int e;
    double c;
    for (int i = 0; i < k; i++) {
        scanf("%d %lf", &e, &c );
        a[e] = c;
    }
    scanf("%d", &k);
    int cnt = 0;
    for (int i = 0; i < k; i++) {
        scanf("%d %lf", &e, &c );
        a[e] += c;
    }
    
    for (int i = 0; i < 1001; i++) {
        if (a[i]) {//即使是浮点数在没有要求精度范围时 这样做也是最正确的
            cnt++;
        }
    }
    
    printf("%d", cnt);
    for (int i = 1000; i >= 0; i--) {
        if (a[i]) {
            printf(" %d %.1lf", i, a[i]);
        }
    }
    printf("\n");
    
    
    return 0;
}


上一篇:1001. A+B Format (20)


下一篇:1005. Spell It Right (20)