c - 如何用链表实现O(M^2N)的两个多项式的乘法?

标签 c algorithm list linked-list polynomial-math

这是“C 中的数据结构和算法分析”练习 3.7 中的练习。 假设在链表中实现了两个多项式。一个有 M 项。另一个有 N 项。练习要求我在 O(M^2N) 中实现两个多项式的乘法(假设 M 是较小的那个)。如何解决?

最佳答案

我可以给你想法。

假设多项式是 1+x+x^3 和 1+x^2。

使用(1,1)--->(1,1)--->(1,3)创建链表P

创建另一个链表 Q (1,1)--->(1,2) 其中 (a,b) 表示 x^b 的系数。

现在对于 P 中的每个节点,将它与 Q 中的每个节点相乘。

如何?我们将创建一个节点 res with (x,y) where

x= P->coeff * Q->coeff.

y=P->exp+Q->exp

将这个新节点添加到将包含答案的多项式中。


在插入 answer 多项式时,您必须记住两件事 -

i) Keep a sorted list (sorted against the exp)(increasing maybe as I have takem increasing here--you can take decreasing also).

ii) Get the correct position in case you add new node and if a node with same exp value exists add the coeff only and delete the node that you are about to insert.

好的!现在打印多项式。

复杂性分析。

关于c - 如何用链表实现O(M^2N)的两个多项式的乘法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29179827/

相关文章:

c - 如何将 Yacc/Bison-Parser 包含到自己的项目中?

C 程序链接错误?

c - 将 Int 添加到字符串

php - 根据元素出现的频率对整数数组进行排序。如果两个元素的频率相同,则它们将按升序打印

ios - 在 iOS 中确定闭合轮廓的算法

c - 在客户端使用 select(..)

algorithm - 递归解决方案的最佳 Big O 时间效率是否始终与最佳空间效率相同?

css - 嵌套有序列表作为多行菜单

list - 如何在 Prolog 中 append 列表?

list - 在Scheme中的列表中添加元素