PAT A1009

ps:这道题挺简单,但是要注意两个多项式乘会出现某一项等于0,这一项就不能输出

我用的map<int,double>存储数据

1009 Product of Polynomials (25)(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: K N1 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < ... < N2 < N1 <=1000.

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

代码:

扫描二维码关注公众号,回复: 2562964 查看本文章

#include<iostream>
#include<map>
using namespace std;
int main()
{
    typedef map<int, double> mapp;
    mapp h1, h2, result;
    int m, n;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        int zhishu;
        double xishu;
        cin >> zhishu >> xishu;
        h1[zhishu] = xishu;
    }
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int zhishu;
        double xishu;
        cin >> zhishu >> xishu;
        h2[zhishu] = xishu;
    }
    for (auto p1 = h1.begin(); p1 != h1.end();p1++)
    for (auto p2 = h2.begin(); p2 != h2.end(); p2++)
    {
        result[p1->first + p2->first] += p1->second*p2->second;
    }
    int co = 0;
    for (auto p1 = result.begin(); p1 != result.end(); p1++)
    {
        if(p1->second!=0)co++;
    }
    cout << co << " ";
    auto p3 = result.end();
    for (p3--; p3 != result.begin(); p3--)
        if(p3->second!=0)printf("%d %.1f ", p3->first, p3->second);
    printf("%d %.1f", p3->first, p3->second);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/luoshiyong123/article/details/81320501