• 注册
当前位置:1313e > 默认分类 >正文

计算多项式

http://acm.buaa.edu.cn/contest/185/problem/B/

一元多项式的计算用链表实现,本题需要注意的是:

1.系数为-1的项只输出负号,例如1-x^4+2x^8-3x^10+15x^14+4x^18

2.当所有输入均为0时,输出结果0

3.如果阶数较多的那个多项式剩下的项的系数为0不要建立新的结点

否则会输出0x^160x^17

#include <cstdio>
#include <cstdlib>
#define MAX 502

typedef struct array
{
	double coef;
	int exp;
} PolyArray[MAX];

typedef struct pnode
{
	double coef;
	int exp;
	struct pnode *next;
}  PolyNode;
void DispPoly(PolyNode *L)
{
	bool first=true;
	PolyNode *p=L->next;
	while (p!=NULL)
	{
		if (first)
			first=false;
		else if (p->coef>0)
			printf("+");
		if (p->exp==0)
			printf("%g",p->coef);
		else if (p->exp==1)
		{
		    if (p->coef==1)
                printf("x");
                else if (p->coef==-1)
                printf("-x");
                else
                printf("%gx",p->coef);
		}
                else
		{
		    if (p->coef==1)
                printf("x^%d",p->exp);
                else if (p->coef==-1)
                printf("-x^%d",p->exp);
                else
                printf("%gx^%d",p->coef,p->exp);
		}

		p=p->next;
	}
	printf("
");
}
void DestroyList(PolyNode *&L)
{
	PolyNode *p=L,*q=p->next;
	while (q!=NULL)
	{
		free(p);
		p=q;
		q=p->next;
	}
	free(p);
}
void CreateListR(PolyNode *&L,PolyArray a,int n)
{
	PolyNode *s,*r;int i;
	L=(PolyNode *)malloc(sizeof(PolyNode));
	L->next=NULL;
	r=L;
	for (i=0;i<n;i++)
	{
		s=(PolyNode *)malloc(sizeof(PolyNode));
		s->coef=a[i].coef;
		s->exp=a[i].exp;
		r->next=s;
		r=s;
	}
	r->next=NULL;
}

void Sort(PolyNode *&head)
{
	PolyNode *p=head->next,*q,*r;
	if (p!=NULL)
	{
		r=p->next;
		p->next=NULL;
		p=r;
		while (p!=NULL)
		{
			r=p->next;
			q=head;
			while (q->next!=NULL && q->next->exp<p->exp)
				q=q->next;
			p->next=q->next;
			q->next=p;
			p=r;
		}
	}
}
void Add(PolyNode *ha,PolyNode *hb,PolyNode *&hc)
{
	PolyNode *pa=ha->next,*pb=hb->next,*s,*tc;
	double c;
	hc=(PolyNode *)malloc(sizeof(PolyNode));
	tc=hc;
	while (pa!=NULL && pb!=NULL)
	{
	        if     (pa->exp>pb->exp)
		{
			s=(PolyNode *)malloc(sizeof(PolyNode));
			s->exp=pa->exp;s->coef=pa->coef;
			tc->next=s;tc=s;
			pa=pa->next;
		}
		else if (pa->exp<pb->exp)
		{
			s=(PolyNode *)malloc(sizeof(PolyNode));
			s->exp=pb->exp;s->coef=pb->coef;
			tc->next=s;tc=s;
			pb=pb->next;
		}
		else
		{
			c=pa->coef+pb->coef;
			if (c!=0)
			{
				s=(PolyNode *)malloc(sizeof(PolyNode));
				s->exp=pa->exp;s->coef=c;
				tc->next=s;tc=s;
			}
			pa=pa->next;
			pb=pb->next;
		}
	}
	if (pb!=NULL) pa=pb;//将阶数较多的那个多项式赋给pa
	while (pa!=NULL)
	{
		        c=pa->coef;
			if (c!=0)//如果剩下的项的系数为0就不建立新的结点
			{
			    s=(PolyNode *)malloc(sizeof(PolyNode));
		            s->exp=pa->exp;s->coef=pa->coef;
                            tc->next=s;tc=s;
			}

		        pa=pa->next;
	}
	tc->next=NULL;
}

int main()
{
    int n,m,i,j;
    while (~scanf("%d%d",&n,&m))
    {
        int g,h;
        g=n+1;
        h=m+1;
        PolyNode *ha,*hb,*hc;
        PolyArray a,b;
    for(i =0; i<MAX;i++ )
    {
        a[i].exp=0;
        a[i].coef=0;
    }
    for(j =0; j<MAX;j++ )
    {
        b[j].exp=0;
        b[j].coef=0;
    }
    for(i =0; i<n+1;i++ )
    {
        scanf("%lf",&a[i].coef);
    }
    for(j=0; j<m+1;j++ )
    {
        scanf("%lf",&b[j].coef);
    }
    for(i =0; i<n+1;i++ )
    {
        a[i].exp=i;
    }
    for(j =0; j<m+1;j++ )
    {
        b[j].exp=j;
    }
        CreateListR(ha,a,g);
	CreateListR(hb,b,h);
	Sort(ha);
	Sort(hb);
	Add(ha,hb,hc);
	if(hc->next!=NULL)
	{
	    DispPoly(hc);
	}
	else
	 printf("0
");
	DestroyList(ha);//要实现多次输入输出必须在每轮结束后销毁链表
	DestroyList(hb);
	DestroyList(hc);
    }
}



本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录
相关推荐