PAT甲级 1009 Product of Polynomials

PAT甲级 1009 Product of Polynomials

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 K K N 1 N_1 N1 a N 1 a_{N_1} aN1 N 2 N_2 N2 a N 2 a_{N_2} aN2 . . . ... ... N K N_K NK a N K a_{N_K} aNK
​​
where K is the number of nonzero terms in the polynomial, N ​ i N_​i Ni and a ​ N ​ i a_{​N​_i} aNi
​​ ( i = 1 , 2 , ⋯ , K ) (i=1,2,⋯,K) (i=1,2,,K) are the exponents and coefficients, respectively. It is given that 1 ≤ K ≤ 10 , 0 ≤ N ​ K < ⋯ < N 2 < N 1 ≤ 1000. 1≤K≤10,0≤N​_K<⋯1K100NK<<N2<N11000.

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

简单的多项式相乘,注意会出现负数以及系数为0的情况,和PAT甲级的1002类似,AC代码如下:

#include
using namespace std;
typedef long long ll;
const int N=1e3+5;
double eps=1e-8;
struct node{
    int exp;
    double coe;
}p1[N],p2[N];
double E[2*N]={0};
int main(){
    int n1,n2;
    cin>>n1;
    for(int i=0;i<n1;i++)
        cin>>p1[i].exp>>p1[i].coe;
    cin>>n2;
    for(int i=0;i<n2;i++)
        cin>>p2[i].exp>>p2[i].coe;
    for(int i=0;i<n1;i++){
        for(int j=0;j<n2;j++){
            E[p1[i].exp+p2[j].exp]+=p1[i].coe*p2[j].coe;
        }
    }
    int cnt=0;
    for(int i=0;i<2*N;i++){
        if(fabs(E[i])>eps) cnt++;
    }
    cout<<cnt;
    for(int i=2*N;i>=0;i--){
        if(fabs(E[i])>eps) printf(" %d %.1f",i,E[i]);
    }
    return 0;
}

你可能感兴趣的:(PAT甲级,模拟)