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
题意:
计算两个多项式a,b相乘的结果;
输入:
a多项式非零项数 指数 系数 … …
b多项式非零项数 指数 系数 … …
输出:
c多项式(a*b)非零项数 指数 系数 … …
思路:
(1)开3个浮点型数组分别存放多项式a,b,c,下标存放指数,对应的值为系数;
(2)计算多项式c的过程中注意对非零项系数的累加.
代码:
#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;
}
词汇:
product 乘积
format 格式
decimal 小数的,十进制的
ps:
70周年了耶,祝大家国庆快乐!
(≧∀≦)ゞ