PTA univariate polynomial multiplication and addition operations

topic

Design of the sum of two functions are univariate polynomial and the product.

Input format:
input divider 2 rows, for each row number of the first 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 format:
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 output 00.
Sample input:
. 4. 3. 1. 4 -5 -2 0 2. 6
. 3 20 is -7. 5. 4. 3. 1
Output Sample:
15 24 -25 22 is 3021-1020-218 356-335144-153 18 is 2 -6. 1
520-44-5291-20

analysis

The two lines are input to the two input array, the answer into a third array.
Do adder array subscript is the index, the content of the coefficient.
Similarly when multiplying, summing index, multiplication coefficient.
Note that the coefficient is 0 can be skipped output.

Code

#include<iostream>
#include<cstring>
using namespace std;
int t1[1005],t2[1005],cj[2005],he[2005];
void show(int q[],int ran){
	int tag=0;
	for(int i=ran;i>=0;i--)
		if(q[i]!=0){
			if(tag)
				cout<<" ";
			tag=1;
			cout<<q[i]<<" "<<i;
		}
	if(!tag)cout<<"0 0";
}
int main(){
	int n1,n2,a,b;
	memset(t1,0,sizeof(t1));
    memset(t2,0,sizeof(t2));
    memset(cj,0,sizeof(cj));
    memset(he,0,sizeof(he));
    cin>>n1;
    for(int i=0;i<n1;i++){
    	cin>>a>>b;
    	t1[b]=a;
	}
	cin>>n2;
	for(int i=0;i<n2;i++){
		cin>>a>>b;
		t2[b]=a;
	}
	for(int i=0;i<=1000;i++){
		for(int j=0;j<=1000;j++){
			cj[i+j]+=t1[i]*t2[j];
		}
	}	
	for(int o=0;o<=1000;o++){
		he[o]+=t1[o]+t2[o];		
	}
	show(cj,2000);
	cout<<endl;
	show(he,1000);
	cout<<endl;
    return 0;
}
Released two original articles · won praise 0 · Views 40

Guess you like

Origin blog.csdn.net/lyphoon9/article/details/104345571