Scott's world.

一元多项式的乘法与加法运算

Word count: 803Reading time: 4 min
2019/07/04 Share

一元多项式的乘法与加法运算

02-线性结构2 一元多项式的乘法与加法运算 (20 分)

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

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

输出样例:

1
2
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

答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include <stdlib.h>
typedef struct PolyNode * Polynoimal;
struct PolyNode{
int coef;
int expon;
struct PolyNode * link;
};
Polynoimal ReadPoly()
{
int n;
Polynoimal L,rear,temp;
L=(Polynoimal)malloc(sizeof(struct PolyNode));
L->link=NULL;
rear=L;
scanf("%d",&n);
while(n--)
{
int c,e;
scanf("%d %d",&c,&e);
Attach(c,e,&rear);
}
rear->link=NULL;
temp=L;
L=L->link;
free(temp);
return L;
}

void Attach(int coef,int expon,Polynoimal* pStr)
{
Polynoimal p=(Polynoimal)malloc(sizeof(struct PolyNode));
p->coef=coef;
p->expon=expon;
p->link=NULL;
(*pStr)->link=p;
*pStr=p;
}

Polynoimal Add(Polynoimal P1,Polynoimal P2)
{
Polynoimal front,rear,temp;
int sum;
rear=(Polynoimal)malloc(sizeof(struct PolyNode));
front=rear;
while(P1&&P2)
{
switch(Compare(P1->expon,P2->expon)){
case 1:
Attach(P1->coef,P1->expon,&rear);
P1=P1->link;
break;
case -1:
Attach(P2->coef,P2->expon,&rear);
P2=P2->link;
break;
case 0:
sum=P1->coef+P2->coef;
if(sum)Attach(sum,P2->expon,&rear);
P1=P1->link;
P2=P2->link;
break;
}
}
for(;P1;P1=P1->link)Attach(P1->coef,P1->expon,&rear);
for(;P2;P2=P2->link)Attach(P2->coef,P2->expon,&rear);
rear->link=NULL;
temp=front;
front=front->link;
free(temp);
return front;
}

Polynoimal Mult(Polynoimal W1,Polynoimal W2)
{
Polynoimal W,Rear,t1,t2,t;
int c,e;
if(!W1||!W2)return NULL;
t1=W1;
t2=W2;
W=(Polynoimal)malloc(sizeof(struct PolyNode));
W->link=NULL;
Rear=W;
while(t2)
{
Attach(t1->coef*t2->coef,t1->expon*t2->expon,&Rear);
t2=t2->link;
}
t1=t1->link;
while(t1)
{
t2=W2;
Rear=W;
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=(Polynoimal)malloc(sizeof(struct PolyNode));
t->coef=c;
t->expon=e;
t->link=Rear->link;
Rear->link=t;
Rear=Rear->link;
}
t2=t2->link;

}
t1=t1->link;
}
t2=W;
W=W->link;
free(t2);
return W;
}

void PrintPoly(Polynoimal P)
{
int flag=0;
if(!P)
{
printf("0 0\n");
return;
}
while(P)
{
if(!flag) flag=1;
else
printf(" ");
printf("%d %d",P->coef,P->expon);
P=P->link;
}
printf("\n");
}
int Compare(int a,int b)
{
if(a>b)return 1;
else if(a<b)return -1;
else return 0;
}
int main()
{
Polynoimal P1,P2,PP,PS;

P1=ReadPoly();
P2=ReadPoly();
PP=Mult(P1,P2);
PrintPoly(PP);
PS=Add(P1,P2);
PrintPoly(PS);
return 0;
}
CATALOG
  1. 1. 一元多项式的乘法与加法运算
    1. 1.1. 答案