A1009. Product of Polynomials (25)-PAT甲级真题

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 N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < … < N2 < N1 <=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

分析

大体上和A1002差不多。
牛客上和PAT上测试用例答案有差异,原因是两个OJ平台精度采用不同的方法,比如2.85取一位一个是2.9,一个是2.8,据说用牛客用double可以过
注意:大数组要全局声明,否则会栈溢出,一般每个进程分配的栈就几十KB。在全局里用的是堆空间,32位系统大至4GB。

#include
using namespace std;
float e[1001]= {0.0},res[2001]= {0.0};
int main() {
    int K,exp;
    float cof;
    for(int i=0; i<2; i++) {
        if(i==0) {
            cin>>K;
            while(K--) {
                cin>>exp>>cof;
                e[exp]+=cof;
            }
        } else {
            cin>>K;
            while(K--) {
                cin>>exp>>cof;
                for(int j=0; j<=1000; j++) {
                    res[j+exp]+=e[j]*cof;
                }
            }
        }
    }
    int cnt=0;
    for(int k=0; k<=2000; k++) {
        if(res[k]!=0) {
            cnt++;
        }
    }
    cout<=0; m--) {
        if(res[m]!=0) {
            printf(" %d %.1f",m,res[m]);
        }
    }
    return 0;
}



你可能感兴趣的:(A1009. Product of Polynomials (25)-PAT甲级真题)