当前位置:   article > 正文

菜鸡奋斗路02-线性结构2 一元多项式的乘法与加法运算

一元多项式的乘法与加法运算

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分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
作者: DS课程组
单位: 浙江大学
时间限制: 200ms
内存限制: 64MB
代码长度限制: 16KB


个人分析:emmmmmm一元多项式加法好像挺容易的,也就是判断俩个多项式中的项,指数小者排在前面,指数相同者进行合并。还好何老师给我准备了小白专场。这道题的方方面面在小白专场里已经讲的相当清楚了。提一下我觉得特殊的点:

专场选择用链表记录多项式,在读入多项式系数时,为了保证代码的一致性(不用为了第一个数据结点写特殊代码),采用了头结点为空结点的处理。

对于菜鸡来说,这次的题目很过瘾(码量很足),虽然是亦步亦趋,跟着小白专场,写了3个小时才写好的程序。但是很激动,自己也能写近200行的代码了。嗯!要越来越厉害!加油加油!!

代码如下:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct itemNode *List;
  4. struct itemNode{
  5. int xishu; //原谅我很low的用了拼音orz
  6. int zhishu;
  7. List next;
  8. };
  9. List Attach(int c,int e,List *pRear)
  10. {
  11. List L;
  12. L=(List)malloc(sizeof(struct itemNode));
  13. L->next=NULL;
  14. L->xishu=c;
  15. L->zhishu=e;
  16. (*pRear)->next=L;
  17. *pRear=L;
  18. }
  19. List Creat()
  20. {
  21. int number,c,e;
  22. List Rear,T,t;
  23. T=(List)malloc(sizeof(struct itemNode));
  24. Rear=T;
  25. scanf("%d",&number);
  26. for(int i=0;i<number;i++)
  27. {
  28. scanf("%d %d",&c,&e);
  29. Attach(c,e,&Rear);
  30. }
  31. t=T;T=T->next;free(t);
  32. return T;
  33. }
  34. List Add(List T1,List T2)
  35. {
  36. List t1,t2,P,Rear,t;
  37. int sum;
  38. t1=T1;t2=T2;
  39. P=(List)malloc(sizeof(struct itemNode));
  40. Rear=P;
  41. while(t1&&t2)
  42. {
  43. if(t1->zhishu==t2->zhishu)
  44. {
  45. sum=t1->xishu+t2->xishu;
  46. if(sum==0)
  47. {
  48. t1=t1->next;
  49. t2=t2->next;
  50. }
  51. else
  52. {
  53. Attach(sum,t1->zhishu,&Rear);
  54. t1=t1->next;
  55. t2=t2->next;
  56. }
  57. }
  58. else if(t1->zhishu>t2->zhishu)
  59. {
  60. Attach(t1->xishu,t1->zhishu,&Rear);
  61. t1=t1->next;
  62. }
  63. else
  64. {
  65. Attach(t2->xishu,t2->zhishu,&Rear);
  66. t2=t2->next;
  67. }
  68. }
  69. while(t1)
  70. {
  71. Attach(t1->xishu,t1->zhishu,&Rear);
  72. t1=t1->next;
  73. }
  74. while(t2)
  75. {
  76. Attach(t2->xishu,t2->zhishu,&Rear);
  77. t2=t2->next;
  78. }
  79. Rear->next=NULL;
  80. t=P;
  81. P=P->next;
  82. free(t);
  83. return P;
  84. }
  85. List Mult(List T1,List T2)
  86. {
  87. //采取先用一多项式第一项乘以二多项式,得到初始结果多项式
  88. List P,t1,t2,Rear,t;
  89. int c,e;
  90. t1=T1;t2=T2;
  91. P=(List)malloc(sizeof(struct itemNode));
  92. Rear=P;
  93. if(!t1||!t2)
  94. return NULL;
  95. while(t2)
  96. {
  97. Attach(t1->xishu*t2->xishu,t1->zhishu+t2->zhishu,&Rear);
  98. t2=t2->next;
  99. }
  100. t1=t1->next;
  101. //再将一多项式各项乘以二多项式,得到结果结点,插入结果多项式
  102. while(t1)
  103. {
  104. t2=T2;
  105. Rear=P;
  106. while(t2)
  107. { e=t1->zhishu+t2->zhishu;
  108. c=t1->xishu*t2->xishu;
  109. while(Rear->next&&e<Rear->next->zhishu)
  110. {
  111. Rear=Rear->next;
  112. }
  113. if(Rear->next&&(Rear->next->zhishu==e))
  114. {
  115. if(c+Rear->next->xishu==0)
  116. {
  117. t=Rear->next;
  118. Rear->next=t->next;
  119. free(t);
  120. }
  121. else
  122. {
  123. Rear->next->xishu+=c;
  124. }
  125. }
  126. else
  127. {
  128. t=(List)malloc(sizeof(struct itemNode));
  129. t->xishu=c;
  130. t->zhishu=e;
  131. t->next=Rear->next;
  132. Rear->next=t;
  133. Rear=Rear->next;
  134. }
  135. t2=t2->next;
  136. }
  137. t1=t1->next;
  138. }
  139. t1=P;P=P->next;free(t1);
  140. return P;
  141. }
  142. void PrintPoly(List T)
  143. {
  144. int flag=0;
  145. if(!T){
  146. printf("0 0\n");
  147. return;
  148. }
  149. while(T){
  150. if(!flag)
  151. flag=1;
  152. else
  153. printf(" ");
  154. printf("%d %d",T->xishu,T->zhishu);
  155. T=T->next;
  156. }
  157. printf("\n");
  158. }
  159. int main()
  160. {
  161. List T1,T2,TT,TS;
  162. T1=Creat();
  163. T2=Creat();
  164. TT=Mult(T1,T2);
  165. PrintPoly(TT);
  166. TS=Add(T1,T2);
  167. PrintPoly(TS);
  168. return 0;
  169. }

测试结果:




总结:欸,最近毕设的事还在折磨着菜鸡,因为身在曹营心在汉,每每动笔的时候都不太写的下去。哪有写代码的时候那种探索->失败->失败->成功的喜悦。不过,这不是不好好写毕设论文的借口,搞定这些事就能一心向码!



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

闽ICP备14008679号