一元多项式的加/减法运算
问题描述 :
假设2个稀疏一元多项式分别由带头结点的有序单链表A和B存储(指数项递增有序)。现要求设计一个算法,实现稀疏一元多项式的加减法计算。要求使用A和B的原存储空间(运算后B不再存在,A链表中保存结果多项式)。输入中的单链表的长度不得在计算算法中利用,仅作为建表使用。
注意:加/减法计算后,如某一项的结果系数为0,则该项要从多项式链表中删除。
输入说明 :
第一行:加/减法选择(0:加法 1:减法)
第二行:一元多项式A的项数
第三行:一元多项式A的各项的系数(系数之间以空格分隔)
第四行:一元多项式A的各项的指数(指数之间以空格分隔)
第五行:一元多项式B的项数
第六行:一元多项式B的各项的系数(系数之间以空格分隔)
第七行:一元多项式B的各项的指数(指数之间以空格分隔)
如果A或B的项数为0,则认为输入的多项式只包含数字“0”,即系数为0,指数也为0。
输出说明 :
第一行:多项式A的第一项的系数、指数(以空格分隔)
第一行:多项式A的第二项的系数、指数(以空格分隔)
…
第n行:多项式A的第n项的系数、指数(以空格分隔) (假设多项式A的项数为n)
(空行)
第一行:多项式B的第一项的系数、指数(以空格分隔)
第一行:多项式B的第二项的系数、指数(以空格分隔)
…
第m行:多项式B的第m项的系数、指数(以空格分隔) (假设多项式B的项数为m)
(空行)
第一行:加/减法计算后,结果多项式A的第一项的系数、指数(以空格分隔)
第一行:加/减法计算后,结果多项式A的第二项的系数、指数(以空格分隔)
…
第p行:加/减法计算后,结果多项式A的第n项的系数、指数(以空格分隔) (假设结果多项式的项数为p)
(多项式之间以空行分隔,如果多项式只包含“0”,则相应的多项式输出"0 0",不包含引号。)
输入范例 :
1
6
7 3 -22 9 5 -8
0 1 7 8 17 100
3
8 22 -9
1 7 8
输出范例 :
7 0
3 1
-22 7
9 8
5 17
-8 100
8 1
22 7
-9 8
7 0
-5 1
-44 7
18 8
5 17
-8 100
需要注意都是0的时候输出“0 0”,还有指针后默认NULL以免空指针异常。
#include <iostream>
using namespace std;
struct listnode{
listnode *next;
int xx;
int zs;
};
listnode * createlist(listnode *head){
int n;
cin>>n;
for(int i = 0;i < n;i++){
listnode *temp = new listnode;
temp->next = head->next;
head->next = temp;
}
listnode *cur1 = new listnode;
listnode *cur2 = new listnode;
cur1 = head;
while(cur1->next){
cur1 = cur1->next;
cin>>cur1->xx;
}
cur2 = head;
while(cur2->next){
cur2 = cur2->next;
cin>>cur2->zs;
}
return head;
}
void print1(listnode *head){
listnode * r1 = new listnode;
int flag = 0;
r1 = head;
while(r1->next != NULL){
r1 = r1->next;
if(r1->xx != 0){
cout<<r1->xx<<" "<<r1->zs<<endl;
flag = 1;
}
}
if(flag == 0){
cout<<"0 0"<<endl;
}
cout<<endl;
}
listnode *together(listnode* head1,listnode *head2,listnode *head3,int flag){
listnode *cur1 = new listnode;
cur1 = head1;
listnode *cur2 = new listnode;
cur2 = head2;
listnode *now = new listnode;
now = head3;
while(cur1->next && cur2->next){
if(cur1->next->zs == cur2->next->zs){
cur1 = cur1->next;
cur2 = cur2->next;
listnode *temp = new listnode;
temp->zs = cur1->zs;
if(flag == 0){
temp->xx = cur1->xx + cur2->xx;
}
if(flag == 1){
temp->xx = cur1->xx - cur2->xx;
}
now->next = temp;
now = temp;
}
else if(cur1->next->zs < cur2->next->zs){
cur1 = cur1->next;
listnode *temp = new listnode;
temp->zs = cur1->zs;
temp->xx = cur1->xx;
now->next = temp;
now = temp;
}
else if(cur1->next->zs > cur2->next->zs){
cur2 = cur2->next;
listnode *temp = new listnode;
temp->zs = cur2->zs;
temp->xx = cur2->xx;
now->next = temp;
now = temp;
}
}
while(cur1->next){
cur1 = cur1->next;
listnode *temp = new listnode;
temp->zs = cur1->zs;
temp->xx = cur1->xx;
now->next = temp;
now = temp;
}
while(cur2->next){
cur2 = cur2->next;
listnode *temp = new listnode;
temp->zs = cur2->zs;
temp->xx = cur2->xx;
now->next = temp;
now = temp;
}
now->next = NULL;
return head3;
}
int main(){
listnode * head1 = new listnode;
listnode * head2 = new listnode;
head1->next = NULL;
head2->next = NULL;
int flag;
cin>>flag;
head1 = createlist(head1);
head2 = createlist(head2);
print1(head1);
print1(head2);
listnode *head3 = new listnode;
head3->next = NULL;
head3 = together(head1,head2,head3,flag);
print1(head3);
}