多项式的和,积(优化版)

该博客介绍了优化后的多项式和与积的计算方法。首先通过排序输入的多项式,然后利用排序后的特性简化求和算法,使其复杂度达到O(max(M,N))。对于积的计算,博主创建了一个M*N项的单链表,再进行排序和合并同类项,整个过程包括排序和去重。代码中包含了多项式的创建、打印、排序、合并同类项以及求和、求积的实现。" 133317570,12547246,R语言批量合并txt文件到Excel,"['数据处理', 'R语言工具', '文件操作']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*****************************************************************************************
*求多项式的和,积(改进版)
*核心思想:输入的多项式先排序,再运算
*多项式(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∪p2
	sortPoly(poly2,NULL);
	p1=poly1->next;
	p2=poly2->next;
	while(p1 && p2) {                                            //计算p1∪p2
		if(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*N
		for(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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值