一元多项式

这篇博客介绍了如何使用C++实现两个多项式的加法和乘法操作。作者提供了创建节点、比较指数、附加节点以及多项式加法和乘法的函数。在加法中,通过switch-case结构处理不同指数的情况;在乘法中,利用了将乘法转化为加法的思想,实现了多项式的高效相乘。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两种空间复杂度不一样
为什么用switch哈哈哈我终于知道啦

#include<bits/stdc++.h>
using namespace std;
typedef struct node{
	int coef;
	int expon;//指数 
	struct node* next;
}node,*pnode;

pnode CreateNode(){
	pnode head,end,p;
	head=new node();
	head->next =NULL;
	end=head;
	int n;cin>>n;
	while(n--){
		p=new node();
		cin>>p->coef>>p->expon;
		end->next =p;
		end=p;
	}
	end->next =NULL;
	return head;
}
int com(int x1,int x2){
	if(x1>x2) return 1;
	if(x1<x2) return 2;
	return 3;
}

void Attach1(int coef,int expon,pnode &p){
	pnode ptr;
	ptr=new node();
	ptr->coef =coef;
	ptr->expon =expon;
	ptr->next =NULL;
	p->next=ptr;
	p=ptr; 
}


//pnode Attach2(int coef,int expon,pnode* p){
//	pnode ptr;
//	ptr=new node();
//	ptr->coef =coef;
//	ptr->expon =expon;
//	ptr->next =NULL;
//	(*p)->next=ptr;
//	*p=ptr; 
//	return *p;
//}


pnode Add1(pnode p1,pnode p2){
	pnode head,p;
	head=new node();
	head->next =NULL;
	p=head;
	p1=p1->next ;p2=p2->next ;
	int sum;
	while(p1&&p2){
		switch(com(p1->expon,p2->expon )){
			case 1:
//				Attach2(p1->coef ,p1->expon ,&p);
				Attach1(p1->coef ,p1->expon ,p);
				p1=p1->next ;
				break;
			case 2:
//				Attach2(p2->coef ,p2->expon ,&p);
				Attach1(p2->coef ,p2->expon ,p);
				p2=p2->next ;
				break;
			case 3:
				sum=p1->coef +p2->coef ;
//				if(sum) Attach2(sum,p1->expon ,&p);
				if(sum) Attach1(sum,p1->expon ,p);
				p1=p1->next ;
				p2=p2->next ;
				break;   
		}
	}
	for(;p1;p1=p1->next )
//		Attach2(p1->coef ,p1->expon ,&p);
		Attach1(p1->coef ,p1->expon ,p);
	for(;p2;p2=p2->next )
//		Attach2(p2->coef ,p2->expon ,&p);
		Attach1(p2->coef ,p2->expon ,p);
	p->next =NULL;
	return head;
} 

pnode Add2(pnode p1,pnode p2){
	pnode head,p;
	head=new node();
	head->next =NULL;
	p=head;
	p1=p1->next ;p2=p2->next ;
	int sum;
	while(p1&&p2){
		switch(com(p1->expon,p2->expon )){
			case 1:
				p->next =p1;
				p1=p1->next ;
				p=p->next ;
				break;
			case 2:
				p->next =p2;
				p2=p2->next ;
				p=p->next ;
				break;
			case 3:
				sum=p1->coef +p2->coef ;
				if(sum){
					p1->coef =sum;
					p->next =p1;
					p=p->next ;
				}
				p1=p1->next ;
				p2=p2->next ;
				break;   
		}
	}
	if(p1) p->next =p1;
	else if(p2) p->next =p2;
	else p->next =NULL;

//为啥错了呢	
//	if(p1) p->next =p1;
//	if(p2) p->next =p2;
//	p->next =NULL; 
	 
	return head;
} 

void TraverseList(pnode head){
	pnode p=head->next;
	while(p){
		cout<<p->coef <<" "<<p->expon ;
		if(p->next !=NULL) cout<<" ";
		if(p->next ==NULL) cout<<endl;
		p=p->next;
	} 
	return;	
} 
int main(){
	pnode p1,p2,p;
	p1=CreateNode();
	p2=CreateNode();

	p=Add2(p1,p2);
	TraverseList(p);
	return 0;
} 

乘法,,
1 将乘法转化成加法运算
将P1当前项(ci,ei)依次乘P2多项式,再加到结果多项式里
2.逐项插入
将P1当前项(c1i,e1i)乘P2当前项(c2i,e2i),并插入到结果多项式 中。

#include<bits/stdc++.h>
using namespace std;
typedef struct node{
	int coef;
	int expon;//指数 
	struct node* next;
}node,*pnode;

pnode CreateNode(){
	pnode head,end,p;
	head=new node();
	head->next =NULL;
	end=head;
	int n;cin>>n;
	while(n--){
		p=new node();
		cin>>p->coef>>p->expon;
		end->next =p;
		end=p;
	}
	end->next =NULL;
	return head;
}
int com(int x1,int x2){
	if(x1>x2) return 1;
	if(x1<x2) return 2;
	return 3;
}

void Attach1(int coef,int expon,pnode &p){
	pnode ptr;
	ptr=new node();
	ptr->coef =coef;
	ptr->expon =expon;
	ptr->next =NULL;
	p->next=ptr;
	p=ptr; 
}


pnode Mult(pnode p1,pnode p2){
	pnode head,t,p,t1,t2;
	int e,c;
	head=new node();
	head->next =NULL;
	p=head;
	t1=p1->next ;t2=p2->next ;
	while(t2){
		Attach1(t1->coef *t2->coef ,t1->expon +t2->expon ,p);
		t2=t2->next ;
	}
	t1=t1->next ;
	while(t1){
		t2=p2->next ;p=head;
		while(t2){
			e=t1->expon +t2->expon ;
			c=t1->coef *t2->coef ;
			while(p->next &&p->next ->expon>e){
				p=p->next ;
			}
			if(p->next &&p->next ->expon==e){
				if(p->next ->coef+c){
					p->next ->coef+=c;
				}
				else{
					t=p->next;
					p=p->next ->next;
					free(t);
				}
			}
			else{
				t=new node();
				t->coef =c;t->expon =e;
				t->next =p->next ;
				p->next =t;p=t ;
			}
			t2=t2->next ;
		} 
		t1=t1->next ;
	}
	return head;
}


void TraverseList(pnode head){
	pnode p=head->next;
	while(p){
		cout<<p->coef <<" "<<p->expon ;
		if(p->next !=NULL) cout<<" ";
		if(p->next ==NULL) cout<<endl;
		p=p->next;
	} 
	return;	
} 
int main(){
	pnode p1,p2,p;
	p1=CreateNode();
	p2=CreateNode();

	p=Mult(p1,p2);
	TraverseList(p);
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值