PAT甲级(Advanced Level)练习题 Product of Polynomials (25)

题目描述

This time, you are supposed to find A*B where A and B are two polynomials.

输入描述:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < … < N2 < N1 <=1000.

输出描述:

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.

输入例子:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

输出例子:

3 3 3.6 2 6.0 1 1.6

题目分析及思路

本题的目的是做到多项式相乘求结果。用结构体数组来存储每个多项式。两个嵌套的循环即可计算得到多项式相乘的结果。关键是如何把指数相同的项相加合并。一开始采用注释掉的代码,用两层for循环去暴力计算求和。提交后发现提示段错误(时间复杂度过高)。最后采用,先把相乘结果的多项式按照指数大小进行排序。然后从前往后找指数相同的合并,时间复杂度又O(N^2)降到了O(N)。注意本题测试样例在牛客网有个坑,1.45小数点保留一位会出现错误。但是在pat官网可以顺利的提交成功。

代码实现(C++版本)

#include <iostream>
#include <algorithm>
#include <iomanip>

using namespace std;
struct Pair{
    int zhishu=0;
    double xishu=0;
};

bool cmp(Pair a,Pair b){
    return a.zhishu>b.zhishu;
}

int main()
{
    int m;
    cin >> m;
    Pair a[m];
    for(int i =0;i<m;i++){
        cin >> a[i].zhishu >> a[i].xishu;
    }
    int n;
    cin >> n;
    Pair b[n];
    for(int i =0;i<n;i++){
        cin >>b[i].zhishu >> b[i].xishu;
    }
    Pair c[m*n];
    int ci = 0;
    int bmax= 0;
    int amax= 0;
    for(int i =0; i<m;i++){
        for(int j =0;j<n;j++){
            c[ci].xishu = a[i].xishu * b[j].xishu;
            c[ci].zhishu = a[i].zhishu +b[j].zhishu;
            if(a[i].zhishu > amax){
                amax = a[i].zhishu;
            }
            if(b[j].zhishu > bmax){
                bmax = b[j].zhishu;
            }
            ci++;
        }
    }
    sort(c,c+m*n,cmp);

    Pair ans[m*n];
    int ai = 0;
    double xishu1 = 0;
    int zhishu1 = bmax+amax;

    for(int i = 0; i < m*n;i++){
        if(c[i].zhishu == zhishu1){
            xishu1+=c[i].xishu;
        }else{
            ans[ai].xishu = xishu1;
            ans[ai].zhishu = zhishu1;
            ai++;
            xishu1 = c[i].xishu;
            zhishu1 = c[i].zhishu;
        }
        if (c[i].xishu ==0||i==m*n-1){
            ans[ai].xishu = xishu1;
            ans[ai].zhishu = zhishu1;
            break;
        }

    }

    /*
    for(int i = bmax+amax; i >= 0;i--){
        double xishu1 =0;
        for(int j= 0;j<m*n;j++){
            if(c[j].zhishu == i){
                xishu1+= c[j].xishu;
            }
        }
        ans[ai].xishu=xishu1;
        ans[ai].zhishu = i;
        ai++;
    }
    */
    sort(ans,ans+m*n,cmp);
    int count = 0;
    for(int i=0;i<m*n;i++){
        if(ans[i].xishu != 0)
            count++;
    }
    cout<<count;

    for(int i=0;i<m*n;i++){
        if(ans[i].xishu != 0)
            cout<<" "<<ans[i].zhishu << " "<<setiosflags(ios::fixed)
            <<setprecision(1)<<ans[i].xishu;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37221167/article/details/88782434
今日推荐