1. 程式人生 > >1009 Product of Polynomials (25 point(s))

1009 Product of Polynomials (25 point(s))

1009 Product of Polynomials (25 point(s))

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​​​​ (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.

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

多項式  f = ax^n + bx^n-1 +...+ cx^0

1.注意點,不能想當然的把係數當成正的來看,所以還是要小心

exponents

指數

coefficients

係數

Polynomials

多項式

#include<iostream>
#include<vector>
#include<stdio.h>
using namespace std;
int main(){
    const int MAXN = 2100;

    float n1[1100];
    float n2[1100];
    float out[MAXN];

    fill(n1,n1+MAXN,0.0);
    fill(n2,n2+MAXN,0.0);
    fill(out,out+MAXN,0.0);

    int N,M;

    cin>>N;
    int max_n1 =0;
    for(int i=0;i<N;i++){
        int temp;
        float flo;
        cin>>temp>>flo;
        n1[temp]+=flo;
        if(temp>max_n1){
            max_n1 = temp;
        }
    }
    cin>>M;
    int max_n2=0;
    for(int i=0;i<M;i++){
        int temp;
        float flo;
        cin>>temp>>flo;
        n2[temp]+=flo;
        if(temp>max_n2){
            max_n2 = temp;
        }
    }

    for(int i=0;i<=max_n1+1;i++){
        for(int j=0;j<=max_n2+1;j++){
            float temp = n1[i]*n2[j];
            int index = i+j;
            out[index] += temp;
            //   cout<<index <<" "<<out[index]<<endl;
        }
    }

    int MAX_index=0;
    int number=0;
    for(int i=0;i<MAXN;i++){
        if(out[i]!=0.0){
            number++;
            MAX_index = i;
        }
    }

    cout<<number;

    for(int i=MAX_index;i>=0;i--){
        if(out[i]!=0.0){
            printf(" %d %.1f",i,out[i]);
        }
    }

    return 0;
}