设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
输出样例:
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
把中国大学mooc上的解法原封不动写下来,视频链接附在下方
浙大数据结构一元多项式小白专场
代码如下
#include<iostream>
using namespace std;
typedef struct PolyNode *Polynomial;
struct PolyNode{
int coef;
int expon;
Polynomial link;
};
void Attach(int c, int e, Polynomial *pRear){
Polynomial P;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P -> coef = c;
P -> expon = e;
P -> link = NULL;
(*pRear) -> link = P;
*pRear = P;
}
Polynomial ReadPoly(){
Polynomial P, Rear, t;
int c, e, N;
cin >> N;
P = (Polynomial)malloc(sizeof(struct PolyNode));
Rear = P;
while(N--){
cin >> c >> e;
Attach(c, e, &Rear);
}
t = P;
P = P -> link;
free(t);
return P;
}
Polynomial Mult(Polynomial P1, Polynomial P2){
Polynomial P, Rear, t1, t2, t;
int c, e;
if(!P1 || !P2) return NULL;
t1 = P1;
t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P -> link = NULL;
Rear = P;
while(t2){
Attach(t1->coef * t2->coef, t1->expon + t2->expon, &Rear);
t2 = t2 -> link;
}
t1 = t1 -> link;
while(t1){
t2 = P2;
Rear = P;
while(t2){
e = t1->expon + t2->expon;
c = t1->coef * t2->coef;
while(Rear->link && Rear->link->expon > e)
Rear = Rear->link;
if(Rear->link && Rear->link->expon == e){
if(Rear->link->coef + c){
Rear->link->coef += c;
}
else{
t = Rear->link;
Rear->link = t->link;
free(t);
}
}
else{
t = (Polynomial)malloc(sizeof(struct PolyNode));
t -> coef = c;
t -> expon = e;
t -> link = Rear -> link;
Rear -> link = t;
}
t2 = t2 -> link;
}
t1 = t1 -> link;
}
t = P;
P = P->link;
free(t);
return P;
}
Polynomial Add(Polynomial P1, Polynomial P2){
Polynomial P, Rear, t, t1, t2;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P -> link = NULL;
t1 = P1; t2 = P2;
Rear = P;
while(t1 && t2){
if(t1->expon > t2->expon){
Attach(t1->coef, t1->expon, &Rear);
t1 = t1 -> link;
}
else if(t1->expon < t2->expon){
Attach(t2->coef, t2->expon, &Rear);
t2 = t2 -> link;
}
else{
if(t1->coef + t2->coef){
Attach(t1->coef + t2->coef, t1->expon, &Rear);
}
t1 = t1->link;
t2 = t2->link;
}
}
while(t1){
Attach(t1->coef, t1->expon, &Rear);
t1 = t1 -> link;
}
while(t2){
Attach(t2->coef, t2->expon, &Rear);
t2 = t2 -> link;
}
t = P;
P = P -> link;
free(t);
return P;
}
void PrintPoly(Polynomial P){
int flag = 0;
if(!P){
cout << "0 0" << endl;
return;
}
while(P){
if(!flag)
flag = 1;
else
cout << " ";
printf("%d %d", P->coef, P->expon);
P = P->link;
}
cout << endl;
}
int main(){
Polynomial P1, P2, PP, PS;
P1 = ReadPoly();
P2 = ReadPoly();
PP = Mult(P1, P2);
PrintPoly(PP);
PS = Add(P1, P2);
PrintPoly(PS);
return 0;
}
测试点全部通过