题目介绍:
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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
代码实现:
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
typedef struct Multiple* Polynomial;
struct Multiple{
int coe;
int index;
Polynomial next;
Multiple(int a,int b):coe(a),index(b),next(nullptr){}
};
void Attach(int c, int e, Polynomial* rear); //插入节点
Polynomial ReadMultiple(int n); //读取多项式
Polynomial AddMulti(Polynomial h1, Polynomial h2); //多项式相加
Polynomial MulMulti(Polynomial h1, Polynomial h2); //多项式相乘
void OutMulti(Polynomial h); //显示多项式
void FreeALL(Polynomial h); //释放节点
int main()
{
int n;
Polynomial m1, m2;
cin >> n;
m1 = ReadMultiple(n);
cin >> n;
m2 = ReadMultiple(n);
Polynomial ans1,ans2;
ans1 = MulMulti(m1,m2);
ans2 = AddMulti(m1,m2);
OutMulti(ans1);
cout << endl;
OutMulti(ans2);
FreeALL(m1);
FreeALL(m2);
FreeALL(ans1);
FreeALL(ans2);
return 0;
}
Polynomial ReadMultiple(int n){
if(n == 0)
return nullptr;
int a,b;
cin >> a >> b;
Polynomial head = new Multiple(a,b);
Polynomial rear = head;
for(int i = 1; i < n; i++){
cin >> a >> b;
Attach(a, b, &rear);
}
return head;
}
Polynomial AddMulti(Polynomial h1, Polynomial h2){
Polynomial h3 = new Multiple(0,0); //结果多项式头结点
Polynomial rear = h3; //尾结点
while(h1 && h2){
if(h1->index > h2->index){
if(h1->coe){
Attach(h1->coe,h1->index, &rear);
}
h1 = h1->next;
}
else if(h2->index > h1->index){
if(h2->coe){
Attach(h2->coe,h2->index, &rear);
}
h2 = h2->next;
}
else{
if(h1->coe + h2->coe){
Attach(h1->coe + h2->coe,h1->index, &rear);
}
h1 = h1->next;
h2 = h2->next;
}
}
for(;h1;h1 = h1->next) Attach(h1->coe,h1->index, &rear);
for(;h2;h2 = h2->next) Attach(h2->coe,h2->index, &rear);
rear = h3;
h3 = h3->next;
delete rear;
return h3;
}
Polynomial MulMulti(Polynomial h1, Polynomial h2){
if(h1 == nullptr || h2 == nullptr)
return nullptr;
Polynomial h3 = new Multiple(0,0); //结果多项式头结点
Polynomial rear = h3; //尾结点
Polynomial pt2 = h2;
while(pt2){
int a = h1->coe * pt2->coe;
int b = h1->index + pt2->index;
if(a){
Attach(a, b, &rear);
}
pt2 = pt2->next;
}
Polynomial FreeH = h3;
h3 = h3->next;
Polynomial pt1 = h1->next;
while(pt1){
pt2 = h2;
rear = FreeH;
while(pt2){
int a = pt1->coe * pt2->coe;
int b = pt1->index + pt2->index;
if(a){
Attach(a, b, &rear);
}
pt2 = pt2->next;
}
rear = FreeH->next;
h3 = AddMulti(h3, rear);
FreeALL(rear);
pt1 = pt1->next;
}
delete FreeH;
return h3;
}
void OutMulti(Polynomial h){
if(h == nullptr){
cout << 0 << ' ' << 0;
return;
}
while(h->next){
cout << h->coe << ' ' << h->index << ' ';
h = h->next;
}
cout << h->coe << ' ' << h->index;
return;
}
void FreeALL(Polynomial h){
if(h == nullptr)
return;
while(h){
Polynomial ptr = h;
h = h->next;
delete ptr;
}
return;
}
void Attach(int c, int e, Polynomial* rear) {
Polynomial temp = new Multiple(c, e);
(*rear)->next = temp;
*rear = temp;
}