1009 Product of 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
AC代码:
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<string>
#include<stack>
#include<cmath>
#include<map>
using namespace std;
#pragma warning(disable:4996)
const int maxn = 10000 + 10;
float ans[maxn];
map<int, float> mp1;
map<int, float> mp2;
int main(){
memset(ans, 0, sizeof(ans));
int k1, k2,e,e1,e2;
float c,c1,c2;
scanf("%d", &k1);
for (int i = 0; i < k1; i++) {
scanf("%d %f", &e, &c);
mp1.insert(make_pair(e, c));
}
scanf("%d", &k2);
for (int i = 0; i < k2; i++) {
scanf("%d %f", &e, &c);
mp2.insert(make_pair(e, c));
}
for (map<int, float> ::iterator it1 = mp1.begin(); it1 != mp1.end(); it1++) {
e1 = it1->first;
c1 = it1->second;
for (map<int, float> ::iterator it2 = mp2.begin(); it2 != mp2.end(); it2++) {
e2 = it2->first;
c2 = it2->second;
ans[e1 + e2] += c1 * c2;
}
}
int countn = 0;
for (int i = 0; i < maxn; i++) {
if (fabs(ans[i] - 0.0) > 1e-6)
countn++;
}
printf("%d", countn++);
for (int i = maxn; i >= 0;i--) {
if (fabs(ans[i] - 0.0) > 1e-6)
printf(" %d %.1f", i,ans[i]);
}
return 0;
}