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
思路分析
多项式乘法,和多项式加法相同的思路,因为输入的数据量很小
可以直接遍历输出
注意:多项式乘法可能乘出多项,依旧进行累加运算即可。
#include<cstdio>
#include<iostream>
#define MAX 1000000
int up1[MAX]={0};
double down1[MAX]={0};
int up2[MAX]={0};
double down2[MAX]={0};
double mul[MAX]={0};
int main()
{
//输入模块
int k1=0,k2=0;
scanf("%d",&k1);
for(int i=0;i<k1;i++)
{
scanf("%d %lf",&up1[i],&down1[i]);
}
scanf("%d",&k2);
for(int i=0;i<k2;i++)
{
scanf("%d %lf",&up2[i],&down2[i]);
}
//计算部分
int temp=0;
for(int i=0;i<k1;i++)
for(int j=0;j<k2;j++)
{
mul[up1[i]+up2[j]]+=down1[i]*down2[j];
}
//输出部分1
int number=0;
for(int i=0;i<MAX;i++)
{
if(mul[i]!=0)number++;
}
printf("%d",number);
//输出部分2
for(int i=MAX-1;i>=0;i--)
{
if(mul[i]!=0)
{
printf(" %d %0.1lf",i,mul[i]);
}
}
}