【Weiss】【第03章】练习3.6:有序多项式相加
【练习3.6】
编写将两个多项式相加的函数。不要毁坏输入数据。用一个链表实现。
如果这两个多项式分别有M项和N项,那么你程序的时间复杂度是多少?
两个按幂次升序的多项式链表,分别维护一个指针。
幂较小者将元素的副本拷贝入节点并加入新链表,指针向后移动。
幂同样大时,将两者的系数相加后的结果加入新链表,两个多项式链表的指针共同向前移动。
其中一个多项式到达尾部时,如另一个未到达尾部,则一边后移一边将相同元素的新节点加入新链表。
时间复杂度O(M+N)。
测试代码如下:
#include <iostream>
#include "linklist.h"
using linklist::List;
using namespace std;
int main(void)
{
//测试多项式加法
List<Poly> a;
a.additem(Poly(, ));
a.additem(Poly(, ));
a.additem(Poly(, ));
a.additem(Poly(, ));
a.additem(Poly(, ));
cout << " ( " << flush;
a.traverse();
cout << ") +\n" << flush;
List<Poly> b;
b.additem(Poly(, ));
b.additem(Poly(, ));
b.additem(Poly(, ));
b.additem(Poly(, ));
b.additem(Poly(, ));
cout << " ( " << flush;
b.traverse();
cout << ") =\n" << flush; List<Poly> answer = linklist::polyplus(a, b);
cout << "\n ( " << flush;
answer.traverse();
cout << ") \n" << flush;
system("pause");
}
实现方面,首先定义了多项式类,以及必要的构造函数及运算符重载,放在poly.h头文件中
#ifndef POLY
#define POLY
#include <iostream>
using namespace std; namespace poly
{
//练习3.6新增,多项式元素定义
class Poly
{
friend bool operator<(const Poly& poly1, const Poly& poly2);
friend bool operator==(const Poly& poly1, const Poly& poly2);
friend bool operator!=(const Poly& poly1, const Poly& poly2);
friend ostream& operator<<(ostream &os, const Poly& poly);
friend Poly operator+(const Poly& poly1, const Poly& poly2);
public:
Poly() = default;
Poly(int coe, int exp) :coefficient(coe), exponent(exp){}
private:
//多项式系数
int coefficient;
//多项式指数
int exponent;
};
//练习3.6新增,多项式元素对应操作符重载,包括<<、<、==、!=,+
bool operator<(const Poly& poly1, const Poly& poly2)
{
return poly1.exponent < poly2.exponent;
}
bool operator==(const Poly& poly1, const Poly& poly2)
{
return poly1.exponent == poly2.exponent;
}
bool operator!=(const Poly& poly1, const Poly& poly2)
{
return !(poly1 == poly2);
}
ostream& operator<<(ostream &os, const Poly& poly)
{
cout << poly.coefficient << "x^" << poly.exponent << flush;
return os;
}
Poly operator+(const Poly& poly1, const Poly& poly2)
{
if (poly1 != poly2)
throw runtime_error("Undefined operation!");
else
{
Poly answer;
answer.coefficient = poly1.coefficient + poly2.coefficient;
answer.exponent = poly1.exponent;
return answer;
}
}
}
#endif
在原链表例程中包含该头文件,并将Poly类与polyplus函数(该函数不是List的成员函数)设为友元
#ifndef LINKLIST
#define LINKLIST
#include <iostream>
#include "poly.h" //包含poly头文件
using namespace std;
using namespace poly; namespace linklist
{
/…………/
template <typename T> class List
{
/…………/
//练习3.6新增,友元声明
friend List<Poly> polyplus(const List<Poly> &inorder_a, const List<Poly> &inorder_b);
friend struct Poly;
/…………/
} /…………/
}
最后是具体实现代码:
//练习3.6新增,不覆盖原传入数据,多项式相加
List<Poly> polyplus(const List<Poly> &inorder_a, const List<Poly> &inorder_b)
{
List<Poly> answer;
Node<Poly>* pwrite = answer.front;
Node<Poly>* pread_a = inorder_a.front;
Node<Poly>* pread_b = inorder_b.front;
while (pread_a != nullptr && pread_b != nullptr)
{
//为实现升序排列,元素较小者加入新的链表,并向前移动指针
if (pread_a->data < pread_b->data)
{
answer.additem(pread_a->data, pwrite);
if (pwrite == nullptr)
pwrite = answer.front;
else
pwrite = pwrite->next;
pread_a = pread_a->next;
}
else if (pread_b->data < pread_a->data)
{
answer.additem(pread_b->data, pwrite);
if (pwrite == nullptr)
pwrite = answer.front;
else
pwrite = pwrite->next;
pread_b = pread_b->next;
}
//元素一样大时共同向前移动指针
else
{
answer.additem(pread_a->data + pread_b->data, pwrite);
if (pwrite == nullptr)
pwrite = answer.front;
else
pwrite = pwrite->next;
pread_a = pread_a->next;
pread_b = pread_b->next;
}
}
//如果其中任一链表未遍历,将剩余的元素复制入新的链表
while (pread_a != nullptr)
{
answer.additem(pread_a->data, pwrite);
if (pwrite == nullptr)
pwrite = answer.front;
else
pwrite = pwrite->next;
pread_a = pread_a->next;
}
while (pread_b != nullptr)
{
answer.additem(pread_b->data, pwrite);
if (pwrite == nullptr)
pwrite = answer.front;
else
pwrite = pwrite->next;
pread_b = pread_b->next;
}
return answer;
}
【Weiss】【第03章】练习3.6:有序多项式相加的更多相关文章
- 第03章 AOP前奏
第03章 AOP前奏 提出问题 ●情景:数学计算器 ●要求 ①执行加减乘除运算 ②日志:在程序执行期间追踪正在发生的活动 ③验证:希望计算器只能处理正数的运算 ●常规实现 ●问题 ○代码混乱:越来越多 ...
- 第03章_基本的SELECT语句
第03章_基本的SELECT语句 1. SQL概述 1.1 SQL背景知识 1946 年,世界上第一台电脑诞生,如今,借由这台电脑发展起来的互联网已经自成江湖.在这几十年里,无数的技术.产业在这片江湖 ...
- suse 12 二进制部署 Kubernetets 1.19.7 - 第03章 - 部署flannel插件
文章目录 1.3.部署flannel网络 1.3.0.下载flannel二进制文件 1.3.1.创建flannel证书和私钥 1.3.2.生成flannel证书和私钥 1.3.3.将pod网段写入et ...
- 【Weiss】【第03章】练习3.7:有序多项式相乘
[练习3.7] 编写一个函数将两个多项式相乘,用一个链表实现.你必须保证输出的多项式按幂次排列,并且任意幂次最多只有一项. a.给出以O(M2N2)时间求解该问题的算法. b.写一个以O(M2N)时间 ...
- 【Weiss】【第03章】练习3.8:有序多项式求幂
[练习3.8] 编写一个程序,输入一个多项式F(X),计算出(F(X))P.你程序的时间复杂度是多少? Answer: (特例:P==0时,返回1.) 如果P是偶数,那么就递归计算((F(X))P/2 ...
- 【Weiss】【第03章】练习3.4、3.5:有序链表求交、并
[练习3.4] 给定两个已排序的表L1和L2,只使用基本的表操作编写计算L1∩L2的过程. [练习3.5] 给定两个已排序的表L1和L2,只使用基本的表操作编写计算L1∪L2的过程. 思路比较简单,测 ...
- 【Weiss】【第03章】练习3.16:删除相同元素
[练习3.16] 假设我们有一个基于数组的表A[0,1...N-1],并且我们想删除所有相同的元素. LastPosition初始值为N-1,但应该随着相同元素被删除而变得越来越小. 考虑图3-61中 ...
- 【Weiss】【第03章】增补附注
基本上每章到增补附注这里就算是结束了. 根据设想,每章的这一篇基本上会注明这一章哪些题没有做,原因是什么,如果以后打算做了也会在这里补充. 还有就是最后会把有此前诸多习题的代码和原数据结构放整理后,以 ...
- 【Weiss】【第03章】练习3.25:数组模拟队列
[练习3.25] 编写实现队列的例程,使用 a.链表 b.数组 Answer: 在这章一开头就已经写了个链表的队列例程了,所以实际上只要做b小题就可以. 数组模拟队列和链表的两点小不同是: ①.数组空 ...
随机推荐
- Python-删除多级目录
def rmdirs(top): for root, dirs, files in os.walk(top, topdown=False): # 先删除文件 for name in files: os ...
- 格式化MyEclipse代码(java、jsp、js)行的长度@修改java代码字体@修改Properties文件编码方式
每次用MyEclipse/Eclipse自带的快捷键Ctrl+shift+f格式化代码时,如果原来的一行代码大于80列,Eclipse就会自动换为多行.如果想格式化代码后不想让代码换行可以通过以下方式 ...
- Dangling meta character '' near index 0
1.replaceAll()方法报错 对字符串使用replaceAll()方法替换 * ? + / | 等字符的时候会报以下异常 Dangling meta character '*' near in ...
- python基础函数、方法
python的函数和方法,通过def 定义: 函数的特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 函数和方法的区别:函数有返回值.方法没有 语法定义: def sayhi():#函数名 p ...
- python os.path 模块常用方法
代码: import os apath = os.path.abspath(__file__) # 绝对路径 dirname = os.path.dirname(apath) basename = o ...
- zctf 2016 android writeup - Jieming的博客
本文为2016年zctf中android的writeup. 首先点我下载题目.使用jeb反编译,对username和password进行部分验证后,再将username+password及一个数据库查 ...
- Docker实战之Redis-Cluster集群
概述 接上一篇Docker实战之MySQL主从复制, 这里是Docker实战系列的第二篇,主要进行Redis-Cluster集群环境的快速搭建.Redis作为基于键值对的NoSQL数据库,具有高性能. ...
- IdentityServer4迁移至3.x版本注意问题详解
前言 之前有一位购买我课程的童鞋利用最新的IdentityServer4版本即对应.NET Core 3.x,发布到生产环境在学习,结果出了一些问题,此前我并未过多关注IdentityServer4升 ...
- CentOS7 部署K8S集群,最新版1.17.3-0
小白在网上找了很多关于k8s集群部署的文档,但是版本老旧,到处踩坑,终于部署成功,记录下过程. 一.准备工作 虚拟机:VMware® Workstation 15 Pro Xhell 6:Xshell ...
- Python——详解collections工具库
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天为大家介绍Python当中一个很好用也是很基础的工具库,叫做collections. collection在英文当中有容器的意思,所以顾 ...