PAT A1009 Product of Polynomials (25 分)

Posted tccbj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT A1009 Product of Polynomials (25 分)相关的知识,希望对你有一定的参考价值。

This time, you are supposed to find A×B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

N?1?? a?N?1???? N?2?? a?N?2???? ... N?K?? a?N?K????

where K is the number of nonzero terms in the polynomial, N?i?? and a?N?i???? (,) are the exponents and coefficients, respectively. It is given that 1, 0.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6
#include <stdio.h>
#include <stdlib.h>

struct poly{
    int exp;
    float a;
};
poly p1[11];
double p2[1000010] = { 0 };
const double ep = 1e-4;
int main(){
    int k1, k2, exp;
    double a;
    scanf("%d", &k1);
    for (int i = 0; i < k1; i++){
        scanf("%d %lf", &exp, &a);
        p1[i].exp = exp;
        p1[i].a = a;
    }
    getchar();
    int count = 0;
    scanf("%d", &k2);
    for (int i = 0; i < k2; i++){
        scanf("%d %lf", &exp, &a);
        for (int j = 0; j < k1; j++){
            double tmp = p1[j].a*a;
            if (p2[exp + p1[j].exp] == 0 && tmp != 0){
                count++;
            }
            p2[exp + p1[j].exp] += tmp;
            if (p2[exp + p1[j].exp] == 0)count--;
        }
    }
    printf("%d", count);
    for (int i = 1000001; i >= 0; i--){
        if (p2[i] != 0.0){
            printf(" %d %.1f", i, p2[i]);
        }
    }
    system("pause");
}

注意点:一开始测试点0一直没通过,查了一下是相乘以后再加起来为0,然后以为是精度问题,发现怎么设置ep都不对,后来才知道原来是count计数错了,变为0的count会多加一次。当时在计算时就统计count是怕超时,结果发现多遍历一遍数count不会超时,反而不会错,应该是测试数据没有很大的。

以上是关于PAT A1009 Product of Polynomials (25 分)的主要内容,如果未能解决你的问题,请参考以下文章

入门模拟A1009 Product of Polynomials(25)

PAT 甲级 A1009 (2019/02/01)

PAT1009:Product of Polynomials

PAT.Product of polynomials(map)

[PTA] PAT(A) 1009 Product of Polynomials (25 分)

PAT Advanced 1009 Product of Polynomials (25分)