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;
    }
     
  • 相关阅读:
    Oracle的序列、视图、索引和表空间
    MySQL存储过程
    MySQL触发器
    MySQL索引和视图
    完整性约束
    Mybatis的核心对象及运行流程
    Mybatis中配置连接池
    IDEA中创建Maven工程整合Mybatis
    Idea中创建JavaWeb工程
    实现整数集合的并、交、差运算
  • 原文地址:https://www.cnblogs.com/bianjunting/p/12492552.html
Copyright © 2011-2022 走看看