数据结构与算法MOOC / 第2周 线性表(Linear Lists) 2:多项式加法

2:多项式加法

总时间限制: 
1000ms 
内存限制: 
5000kB
描述

    我们经常遇到两多项式相加的情况,在这里,我们就需要用程序来模拟实现把两个多项式相加到一起。首先,我们会有两个多项式,每个多项式是独立的一行,每个多项式由系数、幂数这样的多个整数对来表示。

如多项式2x20- x17+ 5x9- 7x7+ 16x5+ 10x+ 22x2- 15

对应的表达式为:2 20 -1 17 5 9 - 7 7 16 5 10 4 22 2 -15 0。  

为了标记每行多项式的结束,在表达式后面加上了一个幂数为负数的整数对。

同时输入表达式的幂数大小顺序是随机的。

我们需要做的就是把所给的两个多项式加起来。

输入
输入包括多行。
第一行整数n,表示有多少组的多项式需要求和。(1 < n < 100)
下面为2n行整数,每一行都是一个多项式的表达式。表示n组需要相加的多项式。
每行长度小于300。
输出
输出包括n行,每行为1组多项式相加的结果。
在每一行的输出结果中,多项式的每一项用“[x y]”形式的字符串表示,x是该项的系数、y 是该项的幂数。要求按照每一项的幂从高到低排列,即先输出幂数高的项、再输出幂数低的项。
系数为零的项不要输出。
样例输入
2
-1 17 2 20 5 9 -7 7 10 4 22 2 -15 0 16 5 0 -1
2 19 7 7 3 17 4 4 15 10 -10 5 13 2 -7 0 8 -8
-1 17 2 23 22 2 6 8 -4 7 -18 0 1 5 21 4 0 -1
12 7 -7 5 3 17 23 4 15 10 -10 5 13 5 2 19 9 -7
样例输出
[ 2 20 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 5 9 ] [ 6 5 ] [ 14 4 ] [ 35 2 ] [ -22 0 ]
[ 2 23 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 6 8 ] [ 8 7 ] [ -3 5 ] [ 44 4 ] [ 22 2 ] [ -18 0 ]
提示
第一组样例数据的第二行末尾的8 -8,因为幂次-8为负数,所以这一行数据结束,8 -8不要参与计算。
#include<stdio.h>
#include<stdlib.h>
struct node
{
	int num;
	int num1;
	struct node *next;
};
int n;
struct node *creat(void)
{
	struct  node *head,*p1,*p2;
	head=NULL;
   	p1=(struct node*)malloc(sizeof(struct node));
	scanf("%d %d",&p1->num,&p1->num1);
	while(p1->num1>=0)
	{
		if(head==NULL)
			head=p1;
		else
			p2->next=p1;
		p2=p1;
		p1=(struct node*)malloc(sizeof(struct node));
		scanf("%d %d",&p1->num,&p1->num1);
	}
	p2->next=NULL;
	return head;

}
void sort(struct node *p1)
{
	struct node *p,*q,*t;
	int temp,temp1;
	for(p=p1;p!=NULL;p=p->next)
	{
		for(q=p;q!=NULL;t=q,q=q->next)
		{	
			if(p->num1<q->num1)
			{
				temp=q->num;
				q->num=p->num;
				p->num=temp;
				temp1=q->num1;
				q->num1=p->num1;
				p->num1=temp1;
			}
			if(p!=q && p->num1==q->num1)
			{
				p->num=q->num+p->num;
				t->next=q->next;
				free(q);
				q=t;
			}
		}

	}
}
struct node *find1(struct node *p1,struct node *p2)
{
	struct node *head,*p4,*p5;
	head=NULL;
	while(p1!=NULL&&p2!=NULL)
	{
		p5=(struct node*)malloc(sizeof(struct node));
		if(p1->num1>p2->num1)
		{
			p5->num=p1->num;
			p5->num1=p1->num1;
			p1=p1->next;
		}
		else if(p1->num1<p2->num1)
		{
			p5->num=p2->num;
			p5->num1=p2->num1;
			p2=p2->next;

		}
		else
		{
			p5->num=p2->num+p1->num;
			p5->num1=p2->num1;
			p2=p2->next;
			p1=p1->next;
		}
		if(head==NULL)
			head=p5;
		else
			p4->next=p5;
		p4=p5;
	}
	while(p1!=NULL)
	{	
		p5=(struct node*)malloc(sizeof(struct node));
		p5->num=p1->num;
		p5->num1=p1->num1;
		if(head==NULL)
			head=p5;
		else
			p4->next=p5;
		p4=p5;
		p1=p1->next;
	}
	while(p2!=NULL)
	{	
		p5=(struct node*)malloc(sizeof(struct node));
		p5->num=p2->num;
		p5->num1=p2->num1;
	
		if(head==NULL)
			head=p5;
		else
			p4->next=p5;
		p4=p5;	
		p2=p2->next;

	}
	p5->next=NULL;
	return head;

}
void print(struct node *head)
{
	int f=0;
	while(head!=NULL)
	{
		if(head->num==0)
			head=head->next;
		else
		{
			if(f==0){
			    printf("[ %d %d ]",head->num,head->num1);
				f=1;
			}
		    else
			{
			    printf(" [ %d %d ]",head->num,head->num1);
			}
	        head=head->next;
		}
	}
	printf("\n");
}
int main()
{
	int i=0;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
		{	
		   struct node *p1,*p5,*p2;
	       p1=creat();
	       p2=creat();
	       sort(p1);
		   sort(p2);
		   p5=find1(p1,p2);
		   print(p5);
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值