02-2 Linear Structure 2 Multiplication and addition of polynomials of one variable (20 points)

Design functions to find the product and sum of two one-variable polynomials respectively.

Input format:

The input is divided into 2 lines. Each line first gives the number of non-zero terms of the polynomial, and then enters the coefficient and exponent of a polynomial non-zero term in an exponential descending manner (the absolute values ​​are all integers not exceeding 1000). Separate numbers with spaces.

Output format:

The output is divided into 2 lines, and the coefficients and exponents of the product polynomial and the non-zero terms of the sum polynomial are output in an exponential descending manner. Numbers should be separated by spaces, but there should be no extra spaces at the end. A zero polynomial should be output 0 0.

Input example:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

Output sample:

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

Let’s start with a wrong version...the first time I did it, I didn’t take a lot of it into account.

#include<iostream>
using namespace std;

struct node{
	int a;
	int n;
};
int main(){
	int x[1001];
	int N1,N2;
	int i,k;
	int flag1=0,flag2=0;
//	node ttt;
//	vector<node>x3;
	cin>>N1;
	node x1[N1];
	for(i=0;i<1001;i++){
		x[i]=0;
	}
	for(i=0;i<N1;i++){
		cin>>x1[i].a>>x1[i].n;
	}
	cin>>N2;
	node x2[N2];
	for(i=0;i<N2;i++){
		cin>>x2[i].a>>x2[i].n;
	}
    for(k=0;k<N2;k++){
    	for(i=0;i<N1;i++){
    		//if(i==N1-1&&k==N2-1) cout<<x2[k].a*x1[i].a<<" "<<x2[k].n+x1[i].n;
    		//else cout<<x2[k].a*x1[i].a<<" "<<x2[k].n+x1[i].n<<" "; 
    		x[x2[k].n+x1[i].n]+=x2[k].a*x1[i].a;
		}
	}
	int t=-1;
	for(i=0;i<1001;i++){
		if(x[i]!=0){
			t = i;
			break;
		}	
	}
	if(i==1001) flag1=1; 
	for(i=1000;i>=0;i--){
		if(x[i]!=0){
			if(i!=t){
				cout<<x[i]<<" "<<i<<" ";
			}
			else cout<<x[i]<<" "<<i;
			
		}
		if(flag1==1){
			cout<<"0 0";
		}
	}
	cout<<endl;		
	i=0; 
	k=0;
//	int flag1=0,flag2=0;
	while(i<N1||k<N2){
		if(i==N1||k==N2){
			if(i==N1){
				if(k==N2-1){
					cout<<x2[k].a<<" "<<x2[k].n;
					k++;
				}
				else{
					cout<<x2[k].a<<" "<<x2[k].n<<" ";
					k++;
				}
				
			}
			else if(k==N2){
				if(i==N1-1){
					cout<<x1[i].a<<" "<<x1[i].n;
					i++;
				}
				else{
					cout<<x1[i].a<<" "<<x1[i].n<<" ";
					i++;
				}		
			}		
		}
		else if(x1[i].n==x2[k].n){
			if(i==N1-1&&k==N2-1){
				cout<<x1[i].a+x2[k].a<<" "<<x1[i].n;
				i++;
				k++;
			}
			else {
				cout<<x1[i].a+x2[k].a<<" "<<x1[i].n<<" ";
				i++;
				k++;
			}	
		}
		else if(x1[i].n>x2[k].n){
			cout<<x1[i].a<<" "<<x1[i].n<<" ";
			i++;	
		}
		else if(x1[i].n<x2[k].n){
			cout<<x2[k].a<<" "<<x2[k].n<<" "; 
			k++;
		}
	}
    
	
	return 0;
}

Later, I mainly made a new one with reference to https://www.cnblogs.com/yuxiaoba/p/8326018.html .

This question mainly needs to consider the tips of outputting 0 0 when zero polynomial is used, and not adding spaces at the end.

		if(s[i]){
			if(cnt){
				cout<<" ";
			}
			cout<<s[i]<<" "<<i;
			cnt++;
	   }
#include<iostream>
using namespace std;
#define N 2002
int x1[N]={0},x2[N]={0},m[N]={0},s[N]={0};
int main(){
	int i,t1,t2,k;
	int T;
	int cnt;
	cin>>T;
	for(i=0;i<T;i++){
		cin>>t1>>t2;
		x1[t2]+=t1;
	}
//	cout<<"1*"<<endl;
	cin>>T;
	for(i=0;i<T;i++){
		cin>>t1>>t2;
		x2[t2]+=t1;
	}
//	cout<<"1*"<<endl;
	for(i=0;i<N;i++){
		if(x1[i]){
			for(k=0;k<N;k++){
			    if(x2[k]){
			    	m[i+k]+=x1[i]*x2[k];
				}
		    }	
		}
	}
	for(i=N;i>=0;i--){
	    if(m[i]){
	    	if(cnt){
	    		cout<<" ";
			}
			cout<<m[i]<<" "<<i;
			cnt++;
		}
		
	}
	if(!cnt){
		cout<<"0 0";
	}
	cout<<endl;
	for(i=0;i<N;i++){
	    if(x1[i]){
	    	s[i]+=x1[i];
		}
		if(x2[i]){
			s[i]+=x2[i];
		}
	}
	cnt=0;
	for(i=N;i>=0;i--){
		if(s[i]){
			if(cnt){
				cout<<" ";
			}
			cout<<s[i]<<" "<<i;
			cnt++;
	   }
	}
	if(!cnt){
		cout<<"0 0";
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/zjy997/article/details/92996394