1009. Product of Polynomials (25)
原题链接
相似题目 1002. A+B for 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
题目大意:
- 给定两个多项式,输出它们的积
- 样例中多项式A有2个项: 2.4 * X^1 + 3.2 * X^0
- 样例中多项式B有2个项: 1.5 * X^2 + 0.5 * X^1
- A*B有3个项 = 3.6 * X^3 + 6.0 * X^2+ 1.6 * X^1
- X^2 表示 X的平方,最好是自己手写计算一下,指数相加,系数相乘
- 精确到小数点后一位
代码:
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int main()
{
vector<double> v(2001);//存储最终系数,指数最大为1000,A*B最大指数是2000
int m;
cin >> m;
vector<int> NK;//指数
vector<double> ank;//系数
for(int i=0; i<m; i++){
int a;
double b;
cin >> a >> b;
NK.push_back(a);
ank.push_back(b);
}
int n;
cin >> n;
for(int i=0; i<n; i++){
int a;//指数
double b;//系数
cin >> a >> b;
for(int j=0; j<ank.size(); j++){
v[a+NK[j]] += b*ank[j];
}
}
vector<int> res;//保存要输出的指数
for(int i=v.size()-1; i>=0; i--){
if(v[i] != 0)
res.push_back(i);
}
cout << res.size();
for(int i=0; i<res.size(); i++)
printf(" %d %.1f", res[i], v[res[i]]);
return 0;
}