zoukankan      html  css  js  c++  java
  • 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:

    N1​​ aN1​​​​ N2​​ aN2​​​​ ... NK​​ aNK​​​​

    where K is the number of nonzero terms in the polynomial, Ni​​ and aNi​​​​ (,) 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;
    }
     
  • 相关阅读:
    RocketMQ源码 — 十、 RocketMQ顺序消息
    RocketMQ源码 — 九、 RocketMQ延时消息
    RocketMQ源码 — 八、 RocketMQ消息重试
    HDU3439 Sequence
    Cipolla算法学习小记
    BZOJ2286: [Sdoi2011]消耗战
    BZOJ4873 寿司餐厅
    BZOJ1718 [Usaco2006 Jan] Redundant Paths 分离的路径
    BZOJ1123 [POI2008]BLO
    BZOJ3996 TJOI2015线性代数
  • 原文地址:https://www.cnblogs.com/bianjunting/p/12492552.html
Copyright © 2011-2022 走看看