PAT.Product of polynomials(map)

Posted bianjunting

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT.Product of polynomials(map)相关的知识,希望对你有一定的参考价值。

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:

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 <iostream>
#include <map>
using namespace std;

map <int, double> mp;


const int maxn = 1000 + 5;

int a1[maxn], a2[maxn];
double b1[maxn], b2[maxn];

int main() {
    int k1, k2;
    cin >> k1;
    for(int i = 0; i < k1; i ++) {
        cin >> a1[i] >> b1[i];
    }
    cin >> k2;
    for(int i = 0; i < k2; i ++) {
        cin >> a2[i] >> b2[i];
    }
    int a3;
    double b3;
    for(int i = 0; i < k1; i ++) {
        for(int j = 0; j < k2; j ++) {
            a3 = a1[i] + a2[j];
            b3 = b1[i] * b2[j];
            if(b3 != 0.0)
                mp[a3] += b3;
        }
    }
    map<int, double> :: reverse_iterator i; // 反向迭代器
    for(i = mp.rbegin(); i!= mp.rend(); i ++) {
        if(i -> second == 0) mp.erase(i -> first); //删除值为零的元素,通过key删除
    }
    printf("%d", mp.size());
    for(i = mp.rbegin(); i != mp.rend(); i ++) {
        printf(" %d %.1f", i -> first, i -> second);
    }
    printf("
");
    return 0;
}

 

 

以上是关于PAT.Product of polynomials(map)的主要内容,如果未能解决你的问题,请参考以下文章

1009 Product of Polynomials

PAT1009:Product of Polynomials

1009 Product of Polynomials (25)(25 分)

1009 Product of Polynomials

1009 Product of Polynomials (25 分)

PTA 甲 1009 Product of Polynomials