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
Meaning of the questions:
Calculating two polynomials a, b multiplied result;
Input:
a non-zero entries polynomial coefficient indices ... ...
B polynomial coefficients nonzero entry indices ... ...
Output:
polynomial c (a * b) non-zero entries coefficient indices ... ...
Ideas:
(1) takes three floating-point arrays were stored polynomials a, b, c, subscript index store, a corresponding coefficient value;
(2) evaluation polynomial c attention accumulated during the non-zero coefficient.
Code:
#include <cstdio>
const int max_=1010;
const int maxn=2010;//两个多项式相乘的指数最大为2000
int main(){
int k,x,count=0;
double y,a[max_]={0},b[max_]={0},c[maxn]={0};
scanf("%d",&k);
for(int i=0;i<k;i++){
scanf("%d %lf",&x,&y);
a[x]=y;//x存放指数,a[x]存放对应的系数
}
scanf("%d",&k);
for(int i=0;i<k;i++){
scanf("%d %lf",&x,&y);
b[x]=y;
for(int j=0;j<max_;j++){
if(a[j]!=0){
//if(c[x+j]==0)count++;//计数非零项(用此方法计数非零项,oj的第一个测试点通不过,因为系数相加为0的情况下也进行计数会出错)
c[x+j]+=a[j]*b[x];//x+j存放相乘后多项式的指数,c[x+j]存放对应的系数
}
}
}
for(int i=0;i<maxn;i++){
if(c[i]!=0)
count++;
}
printf("%d",count);
for(int i=maxn-1;i>=0;i--){
if(c[i]!=0){
printf(" %d %.1f",i,c[i]);
}
}
return 0;
}
vocabulary:
product product
format format
decimal decimal, the decimal
ps:
70th anniversary yeah, I wish you all a happy National Day!
(≧ ∀ ≦)ゞ