两种空间复杂度不一样
为什么用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;
}