1009 Product of Polynomials (25 point(s))
阿新 • • 發佈:2018-11-25
1009 Product of Polynomials (25 point(s))
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
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
多項式 f = ax^n + bx^n-1 +...+ cx^0
1.注意點,不能想當然的把係數當成正的來看,所以還是要小心
exponents
指數
coefficients
係數
Polynomials
多項式
#include<iostream> #include<vector> #include<stdio.h> using namespace std; int main(){ const int MAXN = 2100; float n1[1100]; float n2[1100]; float out[MAXN]; fill(n1,n1+MAXN,0.0); fill(n2,n2+MAXN,0.0); fill(out,out+MAXN,0.0); int N,M; cin>>N; int max_n1 =0; for(int i=0;i<N;i++){ int temp; float flo; cin>>temp>>flo; n1[temp]+=flo; if(temp>max_n1){ max_n1 = temp; } } cin>>M; int max_n2=0; for(int i=0;i<M;i++){ int temp; float flo; cin>>temp>>flo; n2[temp]+=flo; if(temp>max_n2){ max_n2 = temp; } } for(int i=0;i<=max_n1+1;i++){ for(int j=0;j<=max_n2+1;j++){ float temp = n1[i]*n2[j]; int index = i+j; out[index] += temp; // cout<<index <<" "<<out[index]<<endl; } } int MAX_index=0; int number=0; for(int i=0;i<MAXN;i++){ if(out[i]!=0.0){ number++; MAX_index = i; } } cout<<number; for(int i=MAX_index;i>=0;i--){ if(out[i]!=0.0){ printf(" %d %.1f",i,out[i]); } } return 0; }