天天看点

1009. Product of Polynomials (25)

#include <iostream>
using namespace std;

int main(int argc, const char * argv[]) {
    double a[1001] = {0}, b[1001] = {0}, p[2001] = {0};
    int m, n;
    scanf("%d", &m);
    double c;
    int e;
    
    for (int i = 0; i < m; i++) {
        scanf("%d %lf", &e, &c);
        a[e] = c;
    }
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d %lf", &e, &c);
        b[e] = c;
    }
    
    for (int i = 0; i <= 1000; i++) {
        for (int j = 0; j <= 1000; j++) {
            if (a[i] && b[j]) {
                p[i+j] += a[i] * b[j];
            }
        }
    }
    
    int cnt = 0;
    for (int i = 0; i <= 2000; i++) {
        if (p[i]) {
            cnt++;
        }
    }
    
    printf("%d", cnt);
    for (int i = 2000; i >=0 ; i--) {
        if (p[i]) {
            printf(" %d %0.1lf", i, p[i]);
        }
    }
    printf("\n");
    
    return 0;
}
           

简析:采用数组存储。为了简单起见,循环都取题目中的上下界。浮点数的具体问题不存在的。