题目描述 |
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 a~N1~ N2 a~N2~ … NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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
解题思路 |
题目大意为两多项式相乘得出一个新的多项式,设计思路很简单,指数相加,系数相乘.需要注意的地方有:
- 同指数的系数相加后为0,不可以再看作该多项式的一个单项式
- 对于系数,应当精确到小数点后一位(PTA网站上的数据对这个没有严格要求,但牛客网要求比较严格)
代码设计 |
1.输入模块
[任务]
存放一个多项式
[说明]
如果有多项式的话,将会被存放到map中去
[接口]
bool input(map<double,double>& m)
bool input(map<double,double>& m)
{
int n;double ex,co;
if(scanf("%d",&n)!=1) return false;
for(int i=0;i<n;i++){cin>>ex>>co;m[ex]=co;}
return true;
}
2.两多项式相乘模块
[任务]
将给定的两多项式相乘
[说明]
指数相加,系数相乘.对运算后的多项式化简时,同指数的系数进行相加
[接口]
void op(map<double,double> a,map<double,double> b)
void op(map<double,double> a,map<double,double> b)
{
for(map<double,double>::iterator ita=a.begin();ita!=a.end();ita++)
for(map<double,double>::iterator itb=b.begin();itb!=b.end();itb++)
re[ita->first+itb->first]+=ita->second*itb->second;
}
额外话:牛客网上对数据保留一位小数的处理上,一直不得其解
牛客网题目链接
牛客网WA的代码
牛客网AC的代码