PAT Grade A-Analog Type-1009 Product of Polynomials Problem Solving Ideas

1009 Product of Polynomials (25 分)

Insert picture description here

Ideas

It is similar to the previous polynomial addition. There is one thing to note about the product. The initializing array should be twice the size and not out of bounds, otherwise it can be done locally but not online.

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
    
    
    const int max_n = 1005;
    double start[max_n+1] = {
    
    0.0};
    double start2[max_n+1] = {
    
    0.0};
    double start3[2*max_n+1] = {
    
    0.0};
    int n,i,j,a;
    double b;

    scanf("%d",&n);
    for(i =0 ;i<n;i++)
    {
    
    
        scanf("%d%lf",&a,&b);
        start[a] += b;
    }
    scanf("%d",&n);
    for(i =0 ;i<n;i++)
    {
    
    
        scanf("%d%lf",&a,&b);
        start2[a] += b;
    }

    for(i=0;i<max_n+1;i++)
        if(start[i]!=0)
        {
    
    
            for(j=0;j<max_n+1;j++)
            {
    
    
                if(start2[j]!=0)
                    start3[i+j]+=start[i]*start2[j];
            }
        }
    int num = 0;
    for(i =2*max_n;i>=0;i--)
    {
    
    
        if(start3[i]!=0)
        {
    
    
            num+=1;
        }
    }
    printf("%d",num);
    for(i =2*max_n;i>=0;i--)
    {
    
    
        if(start3[i]!=0)
        {
    
    
            printf(" %d %.1lf",i,start3[i]);
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_43999137/article/details/114005894
Recommended