PTA 1009 Product of Polynomials (25 分)

版权声明:如果转载,请注明出处。 https://blog.csdn.net/S_999999/article/details/88044853

1009 Product of Polynomials (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 N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤N​K​​<⋯<N​2​​<N​1​​≤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
#include<iostream>
#include<cstring>
using namespace std;
double ans[3009] ,n1[3009] , n2[3009];
int main ( void ){
	
	
	int k1,k2  ,e;
	scanf("%d",&k1);
	for( int i=1;i<=k1;i++){
		 scanf("%d",&e);
		 scanf("%lf",&n1[e]);
	}
	scanf("%d",&k2);
	for( int i=1;i<=k2;i++){
		 scanf("%d",&e);
		 scanf("%lf",&n2[e]);
	}
	memset( ans , 0,sizeof( ans ) );
	for( int i=0;i<=1008;i++){
		 for( int j=0;j<=1008;j++){
		 	e =   i + j;
		 	ans[e]= ans[e] + n1[i] * n2[j];
		 }
	}
	int flag =0 ,cnt =0;
	for( int i=0;i<=3008;i++)
	     if( ans[i] !=0 )
	         cnt++;
    if( cnt !=0 )
	    printf("%d ",cnt);        
    
	for( int i = 3008; i>=0;i-- ){
		 if( ans[i] !=0 ){
		 	  if( flag != 0 )
		 	      printf(" ");
		 	  printf("%d %0.1lf",i,ans[i]);
		      if( flag ==0 )
			      flag = 1; 
		  }
	 }
	 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/S_999999/article/details/88044853