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×B,其中A和B是两个多项式。
输入规格:
每个输入文件包含一个测试用例。 每个案例占用2行,并且每行包含一个多项式的信息:
K N 1
其中K是多项式中非零项的数量,N i和a N i(i = 1,2,⋯,K)分别是指数和系数。 假设1≤K≤10,0≤N<K <⋯N2 <N1≤1000。
输出规格:
对于每个测试用例,您应该在一行中以与输入相同的格式输出A和B的乘积。 请注意,每行末尾不得有多余的空间。 请精确到小数点后一位。
样本输入:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
样本输出:
3 3 3.6 2 6.0 1 1.6
思路:
用map好,用double测试点4 5很迷,一下一下的段错误,多提交几次看运气就可以过了。。。。但是用map就一直是对的,不会有段错误。。。还是很方便的。。
ps:
测试点0:系数为0要删除
输出小数点要一位
代码:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
int main()
{
map<int,double> m1,m2,ans;
int k,n;
double an;
vector<int> v1,v2; //存
set<int> s; //去重,统计个数,并且排好序了,直接倒着输出就行了
cin>>k;
for(int i=0;i<k;i++)
{
cin>>n>>an;
m1[n]=an;
v1.push_back(n); //第一个多项式
}
cin>>k;
for(int i=0;i<k;i++)
{
cin>>n>>an;
m2[n]=an;
v2.push_back(n); //第二个多项式
}
for(int i=0;i<v1.size();i++) //多项式一
{
for(int j=0;j<v2.size();j++) //多项式2
{
ans[v1[i] + v2[j]] += m1[v1[i]] * m2[v2[j]]; //放入ans
s.insert(v1[i] + v2[j]); //插入新的指数
if (ans[v1[i] + v2[j]]==0){ //测试点0,系数和为0要删除
s.erase(v1[i] + v2[j]);
}
}
}
cout<<s.size();
for(auto it=s.rbegin();it!=s.rend();it++) //倒着输出
printf(" %d %.1f",*it,ans[*it]);
return 0;
}