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:

K 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不会超时,反而不会错,应该是测试数据没有很大的。

原文地址:https://www.cnblogs.com/tccbj/p/10371224.html

时间: 2024-11-08 23:03:00

PAT A1009 Product of Polynomials (25 分)的相关文章

1009 Product of Polynomials (25分) 多项式乘法

1009 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 polynomi

PAT 1009. Product of Polynomials (25)

1009. 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 polynomi

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

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Result Problem portal:1009 Product of Polynomials Description Input Output Sample Sample Input Sample Output Solution Analysis Code #include <bits/stdc++.h>

PAT Advanced 1009 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: K N?1?? a?N?1???? N?2?? a?N?2?

1009 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: K N?1?? a?N?1???? N?2?? a?N?2?

入门模拟A1009 Product of Polynomials(25)

2019-12-21 14:29:21 #include <bits/stdc++.h> #include<math.h> using namespace std; const int MAX_LEN = 10001; int main(){ /*//A多项式,B多项式: vector<int,double> A(MAX_LEN); //初始化 for(int i=0;i<MAX_LEN;++i){ A[i]=0; } hash_map<int,double

1009 Product of Polynomials (25)(25 分)

1009 Product of Polynomials (25)(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 pol

1009. Product of Polynomials (25)——PAT (Advanced Level) Practise

题目信息: 1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 c

pat1009. Product of Polynomials (25)

1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 oc