其他分享
首页 > 其他分享> > 1009 Product of Polynomials (25 分)

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, N​i​​  and a​N​i​​ ​​  
(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

My Code:

#include<iostream>
#include<iomanip>
using namespace std;
int main(){
	int k1,k2,a,i,j,e[10],cnt=0;
	double c[10],b,mult[2010]={};  //多项式指数<=1000,所以设置空间比2000大即可
	cin>>k1;
	for(i=0;i<k1;i++){
		cin>>e[i]>>c[i];
	}
	cin>>k2;
	for(i=0;i<k2;i++){
		cin>>a>>b;
		for(j=0;j<k1;j++){
			mult[a+e[j]]+=b*c[j];  //mult数组下标为相乘结果指数,值为系数
		}
	}
	for(i=0;i<2010;i++){
		if(mult[i]!=0) cnt++;
	}
	cout<<cnt;
	for(i=2009;i>=0;i--){
		if(mult[i]!=0&&cnt){
			cout<<" "<<i<<" "<<fixed<<setprecision(1)<<mult[i]; //保留一位小数
			cnt--;
		}
		if(cnt==0) break;
	}
	return 0;
}

思路:输入第一个多项式分别用两个数组存指数和系数,输入第二个多项式时直接跟第一个多项式各项相乘,数组mult[ ]下标存储最后相乘结果的指数,数组的值存储该指数的系数。

标签:case,25,Product,10,多项式,Polynomials,each,line,mult
来源: https://blog.csdn.net/wwwjkarry/article/details/113796883