多项式的和,积(优化版)
来源:互联网 发布:java socket api 编辑:程序博客网 时间:2024/05/16 10:08
/******************************************************************************************求多项式的和,积(改进版)*核心思想:输入的多项式先排序,再运算*多项式(p1,p2)求和算法: 1.多项式排序O(logMN); * 2.求和过程,简化成了计算p1∪p2 : O(max(M,N)); ***多项式(p1,p2)求积算法: 1.新建一个M*N项的单链表p3保存积: O(M*N);* 2.排序p3:O(logMN);* 3.合并p3中的同类项*author;fangchang*time: 2016/04/03 22:13******************************************************************************************/#include<stdio.h>#include<stdlib.h>typedef struct polyNode { //多项式的结构体int coef; //系数int exp; //指数struct polyNode * next;}*ppolyNode;typedef ppolyNode poly;poly createPoly(); //新建一个多项式 void sortPoly(poly head,poly tail); //排序多项式链表void uniquePoly(poly head); //有序多项式合并同类项void printPoly(poly head); //打印一个多项式void addPoly(); //计算2个多项式相加void multPoly(); //计算2个多项相乘int main () {printf("M+N:\n");addPoly();printf("M*N:\n");multPoly();fflush(stdin);getchar();return 1;}poly createPoly() { //新建一个多项式 (单链表)int i,j;poly head = (poly)malloc(sizeof(struct polyNode));poly pre,cur;if(! head) {printf("createPoly malloc failed.\n");return NULL;}head->coef=0;head->exp=0;head->next=NULL;pre=head;printf("please input coef and exp ,input 0 end input:\n");while(scanf("%d%d",&i,&j) && i!=0) { //输入0 0 结束cur = (poly)malloc(sizeof(struct polyNode));if(! cur) {printf("createPoly malloc failed.\n");return NULL;}cur->coef = i;cur->exp = j;cur->next =NULL;pre->next = cur;pre = cur;}return head;}void printPoly(poly head) { //打印多项式(单链表)if(!head || ! head->next) {return ;}poly p = head->next;while(p) {printf("%d %d\n",p->coef,p->exp);p=p->next;}} void sortPoly(poly head,poly tail) { //单链表排序(快排: log(MN))if ( head->next == tail || head->next->next == tail ) return; poly mid = head->next; poly p = head; poly q = mid; int pivot = mid->exp; poly t = mid->next; while ( t != tail ) { if ( t->exp < pivot ) p = p->next = t; else q = q->next = t; t = t->next; } p->next = mid; q->next = tail; sortPoly( head, mid ); sortPoly( mid, tail ); }void uniquePoly(poly head) { //有序多项式合并同类项if(!head || !head->next || !head->next->next) {return ;}poly p0,p1,p2,p3;p0=head;p1=p0->next;p2=p1->next;while(p1 && p2) {if(p2->exp == p1->exp) {p1->coef+=p2->coef;p3=p2->next;p1->next=p3;free(p2);p2=p3;}p1=p2;p2=p2->next;}p0=head; p1=p0->next;while(p1) {if(0==p1->coef) { //删除系数为0的项p2=p1->next;p0->next=p2;free(p1);p1=p2;}else {p0=p1;p1=p1->next;}}}void addPoly() {printf("please input poly1:\n");poly poly1= createPoly();printf("please input poly2:\n");poly poly2= createPoly();poly p1,p2;int tmp;if(!poly1->next && poly2->next) {printPoly(poly2);return;}if(!poly2->next && poly1->next) {printPoly(poly1);return;}sortPoly(poly1,NULL); //p1,p2的指数(exp)均有序,计算p1+p2变成了计算p1∪p2sortPoly(poly2,NULL);p1=poly1->next;p2=poly2->next;while(p1 && p2) { //计算p1∪p2if(p1->exp==p2->exp) {if(0!=p1->coef + p2->coef) { //合并同类型printf("%d %d\n",(p1->coef + p2->coef),p1->exp); }p1=p1->next;p2=p2->next;}else if(p1->exp>p2->exp) {printf("%d %d\n",p2->coef,p2->exp);p2=p2->next;}else {printf("%d %d\n",p1->coef,p1->exp);p1=p1->next;}}while(p1) {printf("%d %d\n",p1->coef,p1->exp);p1=p1->next;}while(p2) {printf("%d %d\n",p2->coef,p2->exp);p2=p2->next;}}void multPoly() {printf("please input poly1:\n");poly poly1= createPoly();printf("please input poly2:\n");poly poly2= createPoly();poly p1,p2;poly poly3,pre,cur;int coef,exp;if(!poly1->next || !poly2->next) {printf("the result is 0.\n");return ;}poly3=(poly)malloc(sizeof(struct polyNode));if(!poly3) {return ;}poly3->coef=0;poly3->exp=0;poly3->next=NULL;pre= poly3;for(p1=poly1->next;p1;p1=p1->next) { //新建单链表保存M*Nfor(p2=poly2->next;p2;p2=p2->next) {coef=p1->coef * p2->coef ;exp = p1->exp + p2->exp ;cur = (poly)malloc(sizeof(struct polyNode));cur->coef =coef;cur ->exp = exp;cur->next = NULL;pre->next = cur;pre= cur;}}sortPoly(poly3,NULL); //排序uniquePoly(poly3); //去重,合并同类项printPoly(poly3); //打印}end.
0 0
- 多项式的和,积(优化版)
- 求两个多项式的和
- 多项式的乘法和除法
- 多项式的相乘和相加
- 多项式的加法和乘法
- 数据结构---多项式的乘积与和(PTA)
- Matlab实例学习------多项式和非多项式曲线拟合(实例)
- 数据结构初识—— f(x)多项式的算法优化
- hdu 3369 robot 差分求多项式的和
- 多项式大于和渐进大于的区别
- 题目1472:求两个多项式的和
- c++一元多项式的乘法和加法
- 输入两个多项式,计算它们的和。
- 一元多项式的加法和乘法运算
- 最小二乘法的基本原理和多项式拟合
- 一元多项式的加法和乘法
- conv--向量的卷积和多项式乘法
- 一元多项式的乘积与和
- (9) linux shell 命令 -- touch
- JavaScript学习(一)
- linux如何设置无密码登录mysql(如何设置无密码登陆mysql)
- 自定义圆形ProgressBar
- gdb调试带参数程序
- 多项式的和,积(优化版)
- 矩阵转换问题(蛇形打印,蛇形顺序打印,之字打印)c方法
- Python process
- 后端面试题集——网络部分(持续更新中)
- [Properties]——资源配置文件·基本功能
- 软件测试方法
- 模板方法模式
- Bootstrap中元素的padding重写(以超大屏幕Jumbotron为例)
- Python multiprocessing