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

02-线性结构2 一元多项式的乘法与加法运算(20 分)

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

输入格式:

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

输出格式:

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

输入样例:

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

输出样例:

序号输入输出
1
4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
2
2 1 2 1 0
2 1 2 -1 0
1 4 -1 0
2 2
3
2 -1000 1000 1000 0
2 1000 1000 -1000 0
-1000000 2000 2000000 1000 -1000000 0
0 0
4
0
1 999 1000
0 0
999 1000

1、数组方法

#include <stdio.h>
#define N 10000

int main()
{
    int i,j,f,m;
    int a1[N] = {0};
    int a2[N] = {0};
    int b[N] = {0};
    int c[N] ={0};
    scanf("%d",&i);
    while(i--)
    {
    	scanf("%d %d",&m,&f);
	a1[f] += m;
    }
    scanf("%d",&i);
    while (i--)
    {
    	scanf("%d %d",&m,&f);
	a2[f] += m;
    }
    //multiply
    for (i=0;i<N;i++)
    {
    	if(a1[i]!=0)
    	{
            for (j=0;j<N;j++)
	        {
		    if(a2[j]!=0)
		    b[i+j] += a1[i]*a2[j];
		}
	}
    }
    int cnt = 0;
    for (i=N-1;i>=0;i--)
    {
        if(b[i]!=0)
	{
	    if(cnt)
	        printf(" ");
            printf("%d %d", b[i], i);
	    cnt++;
	}
    }
    if(!cnt)
    	printf("0 0");
    printf("\n");
    //Add
    for (i=0;i<N;i++)
    	c[i] += a1[i];
    for (i=0;i<N;i++)
    	c[i] += a2[i];
    cnt = 0;
    for (i=N-1;i>=0;i--)
    {
    	if(c[i]!=0)
    	{
            if(cnt)
		printf(" ");
	    printf("%d %d", c[i], i);
            cnt++;
	}
    }
    if(!cnt)
        printf("0 0");
    return 0;
}
2、链表
#include <stdio.h>
#include <stdlib.h>

typedef struct PolyNode *Polynomial;
struct PolyNode
{
    int coef;
    int expon;
    Polynomial next;
};

Polynomial ReadPoly();
Polynomial Add(Polynomial, Polynomial);
Polynomial Multy(Polynomial, Polynomial);
void Attach(int c, int e, Polynomial *pRear);
void PrintPoly(Polynomial);

int main()
{
	Polynomial L1,L2,L3,L4;
	L1 = ReadPoly();
	L2 = ReadPoly();
	L3 = Multy(L1, L2);
	PrintPoly(L3);
	L4 = Add(L1, L2);
	PrintPoly(L4);
	return 0;
}

Polynomial ReadPoly()
{
	Polynomial P, Temp, Head;
	int N, c, e;
	scanf("%d",&N);
	Head = (Polynomial)malloc(sizeof(struct PolyNode));
	Head->next = NULL;
	P = Head;
	while (N--)
	{
		scanf("%d %d",&c, &e);
		Attach(c, e, &P);
	}
	Temp = Head;
	Head = Head->next;
	free(Temp);
	return Head;
}

void Attach(int c, int e, Polynomial *pRear)
{
	Polynomial P;
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->coef = c;
	P->expon = e;
	P->next = NULL;
	(*pRear)->next = P;
	(*pRear) = P;
}

void PrintPoly(Polynomial P)
{
	int cnt = 0;
	if(!P)
	{
		printf("0 0\n");
		return;
	}
	while (P)
	{
		if(cnt)
			printf(" ");
		printf("%d %d", P->coef, P->expon);
		P = P->next;
		cnt++;
	}
	printf("\n");
}

Polynomial Multy(Polynomial L1, Polynomial L2)
{
	Polynomial P, temp, head, t1, t2;
	int c,e;

	if(!L1 || !L2)
		return NULL;

	t1 = L1; t2 = L2;
	head = (Polynomial)malloc(sizeof(struct PolyNode));
	head->next = NULL;
	P = head;
	while(t2)
	{
		Attach(t1->coef * t2->coef, t1->expon + t2->expon, &P);
		t2 = t2->next;
	}
	t1 = t1->next;
	while (t1)
	{
		t2 = L2; P = head;
		while (t2)
		{	
			c = t1->coef * t2->coef;
			e = t1->expon + t2->expon;
			while (P->next && P->next->expon > e)
				P = P->next;
			if(P->next && P->next->expon == e)
			{
				if(P->next->coef + c)
					P->next->coef += c;
				else
				{
					temp = P->next;
					P->next = temp->next;
					free(temp);
				}
			}
			else
			{
				temp = (Polynomial)malloc(sizeof(struct PolyNode));
				temp->coef = c; temp->expon = e;
				temp->next = P->next;
				P->next = temp;
				P = P->next;
			}
			t2 = t2->next;
		}
		t1 = t1->next;
	}
	t2 = head; head = head->next; free(t2);
	return head;
}



Polynomial Add(Polynomial L1, Polynomial L2)
{
	Polynomial P,temp,head,t1,t2;
	t1 = L1; t2 = L2;
	head = (Polynomial)malloc(sizeof(struct PolyNode));
	head->next = NULL;
	P = head;
	while (t1 && t2)
	{
		if(t1->expon > t2->expon)
		{
			Attach(t1->coef, t1->expon, &P);
			t1 = t1->next;
		}
		else if(t2->expon > t1->expon)
		{
			Attach(t2->coef, t2->expon, &P);
			t2 = t2->next;
		}
		else
		{	
			if(t1->coef + t2->coef == 0)
			{	t1 = t1->next; t2 = t2->next;}
			else
			{
				Attach(t1->coef + t2->coef, t1->expon, &P);
				t1 = t1->next; t2 = t2->next;
			}
		}
	}
	while (t1)
	{	
		Attach(t1->coef, t1->expon, &P);
		t1 = t1->next;
	}
	while (t2)
	{	
		Attach(t2->coef, t2->expon, &P);
		t2 = t2->next;
	}
	P->next = NULL;
	temp = head;
	head = head->next;
	free(temp);
	return head;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值