码迷,mamicode.com
首页 > 其他好文 > 详细

PAT.Product of polynomials(map)

时间:2020-03-14 17:02:41      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:pac   ann   stream   specific   where   pie   pat   col   using   

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("\n");
    return 0;
}

 

 

PAT.Product of polynomials(map)

标签:pac   ann   stream   specific   where   pie   pat   col   using   

原文地址:https://www.cnblogs.com/bianjunting/p/12492552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!