PAT1009

多项式乘法

和多项式加法类似,参考之前多项式加法的代码改改就出来了。

主要是注意一下。最大次数应该会有1000000,然后按照乘法规则来,分配率就没有问题

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string.h>

using namespace std;

float maps[1005];
float mapss[1000005];

int main()
{
    int n,x;
    float y,aa;
    int aN=0;
    cin>>n;
    while (n--)
    {
        cin>>x>>y;
        maps[x] = y;
    }
    cin>>n;
    while (n--)
    {
        cin>>x>>y;
        for (int i = 1000; i >= 0; i--)
        {
            aa=mapss[x+i];
            mapss[x+i] += maps[i] * y;
            if(aa == 0 && mapss[x+i] != 0)
                aN++;
            else if(aa != 0 && mapss[x+i] == 0)
                aN--;
        }
    }
    if(aN == 0)
    {
        cout<<aN;
        return 0;
    }
    else
    {
        cout<<aN<<" ";
    }
    for (int i = 1000000; i >= 0; i--)
    {
        if(aN == 1 && mapss[i] != 0)
        {
            printf("%d %.1f",i,mapss[i]);
            break;
        }
        else if(mapss[i] != 0)
        {
            printf("%d %.1f ",i,mapss[i]);
            aN--;
        }
    }
    return 0;  
}
原文地址:https://www.cnblogs.com/linkstar/p/5699025.html