#include

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 polynomial: K N1 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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


英语不好真是硬伤!!!!
每行代表一个多项式 第一个数是多项式包含的项数 , 后面依次是 指数 系数
计算的过程中 , 先 输入一个多项式, 然后第二个多项式的每一项和第一个多项式相乘
指数相加, 系数相乘
用数组下标表示指数,指数相同的项 合并同类项, 数组下标对应的数值是多项式相乘结果的系数。

最后按照多项式系数从高到低输出多项式即可

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>

using namespace std ; 

#define maxn 3000

struct node{
    int exp;  // 指数
    double cof ; // 系数
};

node poly[2000] ; // 表示多项式的每一项
double result[maxn] ; // i 对应多项式的指数, result[i] 对应多项式的系数
int k1, k2 ; 

int main(){

    memset(result, 0, sizeof(result)) ; 
    // 先输入一个多项式       
    cin >> k1 ; 
    for(int i=0 ; i<k1 ; i++){
        cin >> poly[i].exp >> poly[i].cof ; 
    }

    cin >> k2 ;
    int exp ;
    double cof ; 

    for(int j=0 ; j<k2 ; j++){
        cin >> exp >> cof ; 
        for(int i=0 ; i<k1 ; i++){
            // 第二个多项式的每一项都和 第一个多项式所有项相乘
            result[exp + poly[i].exp] += (cof * poly[i].cof) ; 
        }
    }

    int numbers = 0 ; 
    // 统计 指数 i 的系数 result[i] 不为 0 的所有个数
    for(int i=0 ; i<maxn ; i++){
        if(result[i] != 0 ){
            numbers ++ ; 
        }
    }

    //   按照多项式指数从高到低 输出多项式的所有项
    cout << numbers ; 
    for(int i=maxn-1 ; i>=0 ; i--){
        if(result[i] != 0 ){
            printf(" %d %.1f", i, result[i]) ; 
        }
    }

    return 0 ; 
}

 

posted @ 2018-08-22 00:22  0一叶0知秋0  阅读(122)  评论(0编辑  收藏  举报