Design of the sum of two functions are univariate polynomial and the product.
Input formats:
Input line 2 minutes, respectively, to each row number of the polynomial given non-zero entries, then descending exponential manner enter a nonzero polynomial coefficient and the exponent (integer not exceeding the absolute value of both 1000). Between numbers separated by a space.
Output formats:
Output in 2 rows, respectively descending exponential manner, and outputs the product polynomial and polynomial coefficients and non-zero entries in the index. Between numbers separated by spaces, but the end can not have extra spaces. Zero polynomial should be output 0 0
.
Sample input:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
Sample output:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1 5 20 -4 4 -5 2 9 1 -2 0
#include <iostream> #define M 10000 using namespace std; void print(int data[M]){ bool ini=true; for(int i=M-1;i>=0;i--){ if(data[i]){ if(!ini) cout<<" "; cout<<data[i]<<" "<<i; ini=false; } } if(ini) cout<<"0 0"; cout<<endl; } int main(){ int a[M]={0}; int b[M]={0}; int c[M]={0}; int d[M]={0}; int T,coef,expo; cin>>T; for(int i=0;i<T;i++){ cin>>coef>>expo; a[expo]=coef; } cin>>T; for(int i=0;i<T;i++){ cin>>coef>>expo; b[expo]=coef; } //乘法 for(int i=M-1;i>=0;i--){ if(a[i]){ for(int j=M-1;j>=0;j--){ if(b[j]){ c[i+j]+=(a[i]*b[j]); } } } } //加法 for(int i=M-1;i>=0;i--){ if(a[i]){ d[i]+=a[i]; } } for(int i=M-1;i>=0;i--){ if(b[i]){ d[i]+=b[i]; } } print(c); print(d); system("pause"); return 0; } /** In the debugging process, we should bear in mind is the need to distinguish i and J * /