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的项不输出
这里用map不是很简洁,要遍历两次map才能得到正确答案,用数组似乎简洁一些,但是占用更多的内存
1 #include<iostream> 2 #include<map> 3 using namespace std; 4 struct node{ 5 int exp; 6 double coe; 7 }; 8 map<int, double> mmap; 9 int main(){ 10 node v1[11], v2[11]; 11 int n, m, i; 12 cin>>n; 13 for(i=0; i<n; i++)cin>>v1[i].exp>>v1[i].coe; 14 cin>>m; 15 for(i=0; i<m; i++)cin>>v2[i].exp>>v2[i].coe; 16 for(i=0; i<n; i++){ 17 for(int j=0; j<m; j++){ 18 int exp=v1[i].exp+v2[j].exp; 19 double coe=v1[i].coe*v2[j].coe; 20 if(mmap.count(exp)==0) mmap[exp]=coe; 21 else mmap[exp]+=coe; 22 } 23 } 24 int cnt=0; 25 for(auto it=mmap.begin(); it!=mmap.end(); it++) 26 if(it->second!=0.0) cnt++; 27 cout<<cnt; 28 for(auto it=mmap.rbegin(); it!=mmap.rend(); it++)if(it->second!=0.0)printf(" %d %.1f", it->first, it->second); 29 return 0; 30 }