PAT甲級練習題-Product of Polynomials (25)
阿新 • • 發佈:2019-01-22
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
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.
2 1 2.4 0 3.2 2 2 1.5 1 0.5Sample Output
3 3 3.6 2 6.0 1 1.6
/** 題意: 兩個多項式,係數是浮點數。求乘積 題解: 陣列 a[i] i表指數,a[i]表係數 模擬, **注意!!最大值開到2000,1000+1000 */ #include <cstdio> #include <cassert> #include <iostream> #include <queue> #include <algorithm> #include <stdlib.h> #include <math.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 2000 + 5; double a[maxn]; double b[maxn]; double c[maxn]; int main() { int n,k; double t; scanf("%d",&n); for(int i = 0;i<n;++i){ scanf("%d%lf",&k,&t); a[k] = t; } scanf("%d",&n); for(int i = 0;i<n;++i){ scanf("%d%lf",&k,&t); b[k] = t; } for(int i = 0;i<maxn;++i){ for(int j = 0;j<maxn;++j){ if(a[i]==0 || b[j]==0) continue; c[i+j] += a[i]*b[j]; } } n = 0; vector<int> ans; for(int i = maxn-1;i>=0;--i){ if(fabs(c[i]-0)>1e-8){ n++; ans.push_back(i); } } printf("%d",n); for(int i = 0;i<ans.size();++i){ printf(" %d %.1f",ans[i],c[ans[i]]); } return 0; }