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 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.
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
算法说明:
求多项式乘积AxB,采用数组存储(非必须),下标表示指数,对应的值表示系数。本想着,记录两个多项式可填充的最大下标以减少循环,提高速度,不知为何得不全分,等有时间在调试吧。下面给出多项式乘积的例子:\(f(x),g(x)\)中两系数下标之和为\(k\),对应项乘积的和为\(x^k\)的系数。
\[f(x)=2.4x+3.2\]
\[g(x)=1.5x^2+0.5x\]
\[f(x)·g(x)=3.6x^3+(4.8+1.2)x^2+1.6x\]
// 1009 Product of Polynomials.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
#define Max 1000
int main(int argc, char* argv[])
{
int K,count=0;
int i,j,e;
double c;
double A[Max+1]={0.0};
double B[Max+1]={0.0};
double C[2*Max+1]={0.0};
cin >>K;
for(i=0;i<K;i++){
cin >>e>>c;
A[e]=c;
}
cin >>K;
for(i=0;i<K;i++){
cin >>e>>c;
B[e]=c;
}
for(i=0;i<=Max;i++){
for(j=0;j<=Max;j++){
C[i+j]+=A[i]*B[j];
}
}
for(i=0;i<=2*Max;i++){
if(C[i]!=0.0){
count++;
}
}
cout << count;
for(i=2*Max;i>=0;i--){
if(C[i]!=0.0){
cout <<" " << i
<< setiosflags(ios::fixed)
<< setprecision(1)
<< " " << C[i];
}
}
return 0;
}