题目要求
输入格式:
输入分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 namespace std;typedef struct PNode* List;struct PNode {int index;//指数int xishu;//系数struct PNode* next;};List putinnumber(List list);List multiple(List list1, List list2);List getsum(List l1, List l2);void print(List list);int main() {List list1, list2, list_mul,list_sum;list1 = new struct PNode();//头结点list1->next = NULL;list2 = new struct PNode();list2->next = NULL;list1 = putinnumber(list1);list2 = putinnumber(list2);list_mul = multiple(list1, list2);print(list_mul);list_sum = getsum(list1, list2);print(list_sum);return 0;}List putinnumber(List list) {List newnode;//新节点List k = list;int size;cin >> size;int i = 0;for (; i < size; i++) {newnode = new struct PNode();newnode->next = NULL;cin >> newnode->xishu >> newnode->index;if (newnode->xishu != 0) {k->next = newnode;k = k->next;}}return list;}void print(List list) {List p = list->next;int size=0;if (p != NULL) {while (p) {if (p->xishu != 0) {cout << p->xishu << " " << p->index;size++;List temp = p;while (temp->next&&temp->next->xishu==0) {temp = temp->next;}if (temp->next == NULL)cout << endl;elsecout << " ";p = p->next;}elsep = p->next;}if (size == 0) {cout << "0" << " " << "0"<<endl;}}elsecout << "0" << " " << "0"<<endl;}List multiple(List list1, List list2) {List p = list1->next;//因为有头结点List q = list2->next;List list_mul = new struct PNode();//list_mul->next = NULL;List k=list_mul;List newnode ;while (p) {//拿出list2第一项与list1相乘newnode = new struct PNode();newnode->next = NULL;newnode->index = p->index + q->index;newnode->xishu = p->xishu * q->xishu;k->next = newnode;k = k->next;p = p->next;}q = q->next;List temp = list_mul;//后面需要链表从头开始while (q) {List l1temp = list1->next;while (l1temp) {newnode = new struct PNode();newnode->next = NULL;newnode->index = l1temp->index + q->index;newnode->xishu = l1temp->xishu * q->xishu;while (list_mul->next) {//注意比较的是list->>next的指数,插入if (list_mul->next->index < newnode->index) {//小于List k=list_mul->next;list_mul->next = newnode;newnode->next = k;break;}else if (list_mul->next->index == newnode->index) {//等于list_mul->next->xishu += newnode->xishu;break;}list_mul = list_mul->next;}if (list_mul->next == NULL) {//添加list_mul->next = newnode;}list_mul = temp;l1temp = l1temp->next;}q = q->next;}return temp;}List getsum(List l1, List l2) {List l1temp=l1;List p = l2->next;List temp;while (p) {l1 = l1temp;temp = new struct PNode();temp->index = p->index;temp->xishu = p->xishu;temp->next = NULL;while (l1->next) {if (l1->next->index < temp->index) {List k = l1->next;l1->next = temp;temp->next = k;break;}else if (l1->next->index == temp->index) {l1->next->xishu += temp->xishu;break;}l1 = l1->next;}if (l1->next == NULL) {l1->next = temp;}p = p->next;}return l1temp;}
复习感悟
1.忘记了尾结点的作用,老是对头结点进行操作
2.乘法缺少等于的情况,并且后来补上也忘记了要加上考虑是否等于0,因为加法是对系数等于0的情况进行节点删除,但乘法不行,因为乘法会进行多次比较,最终还是选择了在输出函数进行系数等于零的操作,确定这是最简单的方法
3.第二次写的加法直接对l1和l2进行操作,直接将l2加入到l1,return的也是l1,这样有个问题,返回的是l1指向的最后一个数,所以需要令temp=l1,对temp进行插入,再返回l1,其实和问题1差不多,尽量不要对头结点进行操作,特别是还要返回头结点
4.乘法当插入后忘记了break
改进:
输出函数有所改进,关于最后数不能有空格,学会了用flag标记,除了第一个数,后面的数相当于是空格+数的组合,相比于之前写的,简单很多。
加法函数有了自己的思想,直接将list2插入到list1
乘法函数之前是将list2第一项与list1相乘,这次写就发现其实不用
print函数:
void print(List list) {int flag = 0;if (!list->next) {cout << 0 << " " << 0 << endl; return; }List q = list->next;while (q) {if (flag == 0) {if (q->xishu != 0)cout << q->xishu << " " << q->index;q = q->next;flag = 1;}else {if (q->xishu != 0)cout << " " << q->xishu << " " << q->index;q = q->next;}}cout << endl;}
加法函数:
List getsum(List l1, List l2) {List newnode;List temp = l1;while (temp->next && l2->next) {if (l2->next->index > temp->next->index) {newnode = new node;newnode->index = l2->next->index;newnode->xishu = l2->next->xishu;newnode->next = temp->next;temp->next = newnode;l2 = l2->next;temp = temp->next;}else if (l2->next->index == temp->next->index) {newnode = new node;newnode->index = l2->next->index;newnode->xishu = l2->next->xishu + temp->next->xishu;if (newnode->xishu != 0) {newnode->next = temp->next->next;temp->next = newnode;l2 = l2->next;temp = temp->next;}else {temp->next = temp->next->next;l2 = l2->next;}}else{temp = temp->next;}}if (!temp->next) temp->next = l2->next;return l1;}
乘法函数:
List multiple(List list1, List list2) {List p = list2->next;//因为有头结点List q = list1->next;List l3 = new node;l3->next = NULL;List newnode;while (p) {//拿出list2第一项与list1相乘q = list1->next;while (q) {newnode = new node;newnode->next = NULL;newnode->index = p->index + q->index;newnode->xishu = p->xishu * q->xishu;List l = l3;while (l->next) {if (l->next->index < newnode->index) {List temp = l->next;newnode->next = temp;l->next = newnode;break;}else if (l->next->index == newnode->index){l->next->xishu += newnode->xishu;break;}else {l = l->next;}}if (!l->next) {l->next = newnode;l = l->next;}q = q->next;}p = p->next;}return l3;}