A polynomial of degree n is of the form P(x) = anxn + an-1xn-1 + … + a0. Given two polynomials f(x) and g(x) of degrees n and m respectively, write a program to find the polynomial h(x) given by,
h(x) = f(x) * g(x)
h(x) = f(x) * g(x)
INPUT: Line 1 contains n and m separated by space.
Line 2 contains the coefficients an, an-1…, a0 of f(x) separated by space.
Line 3 contains the coefficients bm, bm-1…, b0 of g(x) separated by space.
Line 2 contains the coefficients an, an-1…, a0 of f(x) separated by space.
Line 3 contains the coefficients bm, bm-1…, b0 of g(x) separated by space.
OUTPUT: The degree of h(x) followed by the coefficients ck, ck-1…, c0 of h(x) in next line separated by space.
Constraints:
1 <= n <= 10
1 <= ai <= 100
#include<stdio.h>
1 <= n <= 10
1 <= ai <= 100
#include<stdio.h>
#include<string.h>
int main()
{
int n,m,i,j,x,a[10],b[10],c[10][10],d[20]={0};
scanf("%d %d",&n,&m);
for(i=n;i>=0;i--)
scanf("%d",&a[i]);
for(i=m;i>=0;i--)
scanf("%d",&b[i]);
for(i=n;i>=0;i--)
{
for(j=m;j>=0;j--)
{
c[i][j]=a[i]*b[j];
}
}
for(x=0;x<=m+n;x++)
{
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(i+j==x)
{
d[x] += c[i][j];
}
}
}
}
printf("%d\n%d",m+n,d[m+n]);
for(x=m+n-1;x>=0;x--)
{
printf(" %d",d[x]);
}
return 0;
}
No comments:
Post a Comment