当前位置:   article > 正文

线性结构2 一元多项式的乘法与加法运算

int ploynomial_add1(linklist f1, linklist f2, linklist *f) { lnode *p_f1 =

题目

02-线性结构2 一元多项式的乘法与加法运算(20 分)
设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。

输入样例:

  1. 4 3 4 -5 2 6 1 -2 0
  2. 3 5 20 -7 4 3 1

输出样例:

  1. 15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
  2. 5 20 -4 4 -5 2 9 1 -2 0

分析

使用链表保存多项式的系数和指数,因为输入时是有序的,因此多项式的加类似于有序链表的排序,多项式的乘法可以在插入时确定要插入的位置,也可以固定插入到链表尾,最后对链表排序。

AC代码

  1. #include "bits/stdc++.h"
  2. using namespace std;
  3. typedef struct PloyNode *Ploynomial;
  4. struct PloyNode
  5. {
  6. int coef; //系数
  7. int expon; //指数
  8. Ploynomial link;
  9. };
  10. void Attach(int c, int e, Ploynomial *pRear) { //因为要修改指针的值,因此需要传入指针的指针
  11. Ploynomial p;
  12. p = (Ploynomial)malloc(sizeof(struct PloyNode));
  13. p->coef = c;
  14. p->expon = e;
  15. p->link = NULL;
  16. (*pRear)->link = p;
  17. *pRear = p; //修改pReal
  18. }
  19. Ploynomial ReadPloy() {
  20. Ploynomial p, Rear, t;
  21. int c, e, N;
  22. cin >> N;
  23. if (!N) return NULL;
  24. p = (Ploynomial)malloc(sizeof(struct PloyNode));
  25. p->link = NULL;
  26. Rear = p;
  27. while (N--) {
  28. cin >> c >> e;
  29. Attach(c, e, &Rear);//因为要修改指针的值,因此必须传入地址
  30. }
  31. //删除无用的头结点
  32. t = p; p = p->link; free(t);
  33. return p;
  34. }
  35. Ploynomial Add(Ploynomial p1, Ploynomial p2) {
  36. if (!p1) return p2;
  37. if (!p2) return p1;
  38. Ploynomial t1, t2, p, Rear, t;
  39. t1 = p1; t2 = p2;
  40. p = (Ploynomial)malloc(sizeof(struct PloyNode));
  41. p->link = NULL;
  42. Rear = p;
  43. while (t1&&t2) {
  44. if (t1->expon == t2->expon) {
  45. int sum = t1->coef + t2->coef;
  46. if (sum) Attach(sum, t1->expon, &Rear);
  47. t1 = t1->link;
  48. t2 = t2->link;
  49. }
  50. else if (t1->expon > t2->expon) {
  51. Attach(t1->coef, t1->expon, &Rear);
  52. t1 = t1->link;
  53. }
  54. else {
  55. Attach(t2->coef, t2->expon, &Rear);
  56. t2 = t2->link;
  57. }
  58. }
  59. while (t1) {
  60. Attach(t1->coef, t1->expon, &Rear);
  61. t1 = t1->link;
  62. }
  63. while (t2) {
  64. Attach(t2->coef, t2->expon, &Rear);
  65. t2 = t2->link;
  66. }
  67. t = p; p = p->link; free(t);//删除无用的头结点
  68. return p;
  69. }
  70. Ploynomial Mult(Ploynomial p1, Ploynomial p2) {
  71. if (!p1 || !p2) return NULL;
  72. Ploynomial t1, t2, p, Rear, t;
  73. t1 = p1;
  74. p = (Ploynomial)malloc(sizeof(struct PloyNode));
  75. p->link = NULL;
  76. Rear = p;
  77. while (t1) {
  78. t2 = p2;
  79. while (t2) {
  80. Attach(t1->coef*t2->coef, t1->expon + t2->expon, &Rear);
  81. t2 = t2->link;
  82. }
  83. t1 = t1->link;
  84. }
  85. t = p; p = p->link; free(t);
  86. // 用冒泡把多项式按照指数降序排列
  87. t1 = p;
  88. while (t1->link) {
  89. t2 = p;
  90. while (t2->link) {
  91. if (t2->expon < t2->link->expon) {//交换位置
  92. swap(t2->expon, t2->link->expon);
  93. swap(t2->coef, t2->link->coef);
  94. }
  95. else if (t2->expon == t2->link->expon) {//指数相等,合并
  96. t = t2->link;
  97. t2->coef += t->coef;
  98. t2->link = t->link;
  99. free(t);
  100. }
  101. t2 = t2->link;
  102. }
  103. t1 = t1->link;
  104. }
  105. return p;
  106. }
  107. void PrintPloy(Ploynomial p) {
  108. if (!p) {
  109. cout << "0 0";
  110. return;
  111. }
  112. bool ans = true;
  113. if (p->coef) {
  114. cout << p->coef << ' ' << p->expon;
  115. ans = false;
  116. }
  117. p = p->link;
  118. while (p) {
  119. if (p->coef) {
  120. cout << ' ' << p->coef << ' ' << p->expon;
  121. ans = false;
  122. }
  123. p = p->link;
  124. }
  125. if (ans) cout << "0 0";
  126. }
  127. int main() {
  128. Ploynomial p1, p2, pp, ps;
  129. p1 = ReadPloy();
  130. p2 = ReadPloy();
  131. pp = Mult(p1, p2);
  132. PrintPloy(pp);
  133. cout << endl;
  134. ps = Add(p1, p2);
  135. PrintPloy(ps);
  136. return 0;
  137. }

转载于:https://www.cnblogs.com/lepeCoder/p/7623883.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/342831
推荐阅读
相关标签
  

闽ICP备14008679号