PAT Advanced 1009 Product of Polynomials(25)
作者:互联网
题目描述:
This time, you are supposed to find A×B where A and B are two polynomials.
Input Specification:
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<iostream>
using namespace std;
double nk,a[1010] = {0.0},c[2020]={0.0}; // nk 系数 数组下标 次数
int main()
{
int k1,k2,sum=0,n;
cin >> k1;
for(int i = 0 ; i < k1 ; i++)
{
cin >> n >> nk ;
a[n] = nk ;
}
cin >> k2;
for(int i = 0 ; i < k2 ; i++)
{
cin >> n >> nk ;
for(int j = 0 ; j <= 1000 ; j++ )
c[ n + j ] += nk * a[j] ;
}
for(int i = 0 ; i <= 2000 ; i++ )
if(c[i])
sum++;
cout << sum ;
for(int i = 2000 ; i >= 0 ; i-- )
if(c[i])
printf(" %d %.1lf", i, c[i]);
return 0;
}
标签:25,Product,nk,PAT,int,多项式,cin,k2,k1 来源: https://www.cnblogs.com/yztozju/p/16580454.html