题目要求

输入格式:
输入分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;
}

一元多项式的乘法与加法运算相关推荐

  1. 7-2一元多项式的乘法与加法运算

    title: "7-2一元多项式的乘法与加法运算(20" date: 2018-06-14T01:09:46+08:00 tags: [""] categori ...

  2. PTA:编程题:7-1 一元多项式的乘法与加法运算 (20 分)

    大一下半期数据结构 数据结构题目集 一元多项式的乘法与加法运算 设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项 ...

  3. 7-2 一元多项式的乘法与加法运算 (20 分)

    7-2 一元多项式的乘法与加法运算 (20 分) 设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝 ...

  4. 浙大数据结构题集02-线性结构2 一元多项式的乘法与加法运算python版

    浙大数据结构题集02-线性结构2 一元多项式的乘法与加法运算python版 设计函数分别求两个一元多项式的乘积与和. 本文用链表做的 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数 ...

  5. PTA->一元多项式的乘法与加法运算

    一元多项式的乘法与加法运算 1.问题描述 2.问题分析 2.1定义多项式数据结构结点数据PolyNode 2.2将数据结点连接到多项式后面Attach 2.3读入多项式数据结点ReadPoly 2.4 ...

  6. 习题3.6 一元多项式的乘法与加法运算 (20 分)(有测试点具体数据)c语言链表版本

    习题3.6 一元多项式的乘法与加法运算 (20 分) 设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数 ...

  7. PTA 7-1 一元多项式的乘法与加法运算 (20 分)

    设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. ...

  8. mooc浙大数据结构PTA习题之一元多项式的乘法与加法运算

    设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. ...

  9. 一元多项式的乘法与加法运算_行测数学运算之速算与技巧

    公务员考试中,计算能力是数量关系部分的基本能力,几乎所有题目最后都会转化成对计算规律的考查. (1)凑整法 凑整法:是根据数的特点,借助于数的组合.分解以及四则运算等规律,将几个数字凑成整十.整百.整 ...

  10. 7-2 一元多项式的乘法与加法运算 (20 分)(思路加详解+map做法)map真香啊 各个测试点的用例子 来吧宝贝!

    一:题目 设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以 ...

最新文章

  1. 『计算机视觉』经典RCNN_其一:从RCNN到Faster-RCNN
  2. ASP.NET 调味品:AJAX
  3. linux基础命令学习
  4. 物联网时代如何管理上百万设备?找风河DLM就对了!
  5. Oracle多行函数
  6. php外联样式,css外联样式不起作用怎么办
  7. 论文浅尝 | 一个模型解决所有问题:实体和事件的神经联合模型
  8. swift 的init_Swift init()
  9. vue____后台管理系统搭建(推荐,懒得自己写了)
  10. 上位机plc编程入门_PLC与上位机的通信-plc上位机程序开发
  11. [转]Xmanager连接Linux远程桌面(后面添加了自己的部分)
  12. 最新CCF会议|2022-2023顶会会议时间+投稿时间+官网链接(视觉+多媒体+数据挖掘+数据库+通用人工智能)
  13. Java IO流学习总结(一)—— IO流分类和常用IO流汇总
  14. (二)进程管理之进程状态及组织方式
  15. 世界著名汽车公司谱系
  16. VScode必备插件、Emmet语法、面试题更新——用到老
  17. Java程序员高效开发必备的5大工具,IDEA黑色主题让程序员爽翻!
  18. Cocos Creator_实现策略_多点触控之触控管理器监听单例_TypeScript
  19. 英语单词常用词根(三)
  20. GPT-4 太强了!一夜醒来,Excel 到 PPT 动嘴就能做!

热门文章

  1. 解决d3dcompiler_42.dll错误找不到等问题
  2. jlink怎么调试linux程序_linux下用eclipse + GDBserver + JLINK 在线调试(ARM11)
  3. Transfer 穿梭框
  4. 水电装修需要注意的16个细节
  5. DVWA远程文件包含防御
  6. 2017第27届中国华东进出口商品交易会(2017上海华交会)会刊(参展商名录)
  7. Maven项目和Gradle项目相互转换
  8. php对接一网通,先安一网通平台
  9. OPT小讲堂 ∣ SciSmart图像识别之条形码识别、二维码识别
  10. Lua⭐️迭代器pairs、ipairs ; 自定义迭代器