美文网首页
多项式乘法 | 1009 Product of Polynomi

多项式乘法 | 1009 Product of Polynomi

作者: zilla | 来源:发表于2019-01-23 15:55 被阅读0次

    1009 Product of Polynomials

    折腾半天ac不了,试了图中的例子,非零项计数出错,还以为是浮点数比较出了问题,想fabs()再与const float ZERO=1e-6比较判断是否为0
    然鹅。。。
    partially accected的主要原因是,只考虑了原先这个幂次的系数是不是0,没有在运算后在判断一下是否为0。

    Screen Shot 2019-01-23 at 3.29.08 PM.png

    附:两个测试例子

    • 1 1000 0.1
      1 0 0.2
    • 2 3 1 2 -1
      2 1 1 0 1
    #include <cstdio>
    #define N 1005
    int main() {
        int an, bn, ex, high_ex1, high_ex2, low_ex1, low_ex2;
        float co, a_poly[N] = {0.0f}, b_poly[N] = {0.0f}, res[2 * N] = {0.0f};
        scanf("%d%d%f", &an, &ex, &co);
        high_ex1 = ex;
        a_poly[ex] = co;
        for (int i = 1; i < an; ++i) {
            scanf("%d%f", &ex, &co);
            a_poly[ex] = co;
        }
        low_ex1 = ex;
        scanf("%d%d%f", &bn, &ex, &co);
        high_ex2 = ex;
        b_poly[ex] = co;
        for (int i = 1; i < bn; ++i) {
            scanf("%d%f", &ex, &co);
            b_poly[ex] = co;
        }
        low_ex2 = ex;
        int cnt_term = 0;
        for (int j = low_ex1; j <= high_ex1; ++j) {
            if (a_poly[j] != 0.0f)
                for (int i = low_ex2; i <= high_ex2; ++i) {
                    if (b_poly[i] != 0) {
                        if (res[i + j] == 0.0f) {
                            cnt_term++;
                        }
                        res[i + j] += a_poly[j] * b_poly[I];
                        if (res[i + j] == 0.0f) {
                            cnt_term--;
                        }
                    }
                }
        }
        printf("%d ", cnt_term);
        for (int k = high_ex1 + high_ex2; cnt_term > 1 && k > low_ex1 + low_ex2; --k) {
            if (res[k] != 0.0f) {
                cnt_term--;
                printf("%d %.1f ", k, res[k]);
            }
        }
        printf("%d %.1f", low_ex1 + low_ex2, res[low_ex1 + low_ex2]);
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:多项式乘法 | 1009 Product of Polynomi

          本文链接:https://www.haomeiwen.com/subject/djtsjqtx.html