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;
    }
     
  • 相关阅读:
    Mysql:Group Replication & Replication
    使用winsw包装服务将nginx包装为Windows服务
    Nginx的一些常用配置
    在ASP.NET Core 2.0中使用Facebook进行身份验证
    展现层实现增删改查
    ABP创建应用服务
    ABP领域层定义仓储并实现
    ABP领域层创建实体
    通过模板创建一个ABP项目
    Android UI组件:布局管理器
  • 原文地址:https://www.cnblogs.com/bianjunting/p/12492552.html
Copyright © 2011-2022 走看看