7-2 一元多项式的乘法与加法运算 (20 分)

#include<bits/stdc++.h>
using namespace std;

struct node{
	int x,y;
}a[3000], b[3000], c[1000010],d[1000010];

int al, bl, cl, dl;

struct rule{
	bool operator()(const node &a, const node &b)const
	{
		return a.x > b.x;
	}
};
void create(node a[],int len)
{
	for (int i = 1; i <= len; i ++ )
	{
		cin >> a[i].y >> a[i].x;
	}
}

void jia()
{
	memset (c,0,sizeof(c));
	cl = 0;
	int i = 1, j = 1;
	while(i <= al && j <= bl)
	{
		if(a[i].x == b[j].x)
		{
			c[++cl].x = a[i].x;
			c[cl].y = a[i].y + b[j].y;
			i++,j++;
		}
		else if (a[i].x > b[j].x)
		{
			c[++cl].x = a[i].x;
			c[cl].y = a[i].y;
			i++;
		}
		else
		{
			c[++cl].x = b[j].x;
			c[cl].y = b[j].y;
			j++;
		}
	}
	
	while (i <= al)
	{
		c[++cl].x = a[i].x;
		c[cl].y = a[i].y;
		i++;
	}
	
	while (j <= bl)
	{
		c[++cl].x = b[j].x;
		c[cl].y = b[j].y;
		j++;
	}
}

void print (node c[], int cl)
{
	int cnt = 0;
	for (int i = 1; i<=cl; i ++ )
		if (c[i].y)
			cnt ++ ;
	if (!cnt)	cout << "0 0" << endl;
	else
	{
		for (int i = 1; i <= cl; i ++ )
		{
			if (c[i].y)
			{
				cnt -- ;
				cout << c[i].y << " " << c[i].x;
				if (cnt)	cout << " ";
				else
				{
					cout << endl;
					break;
				}
			}
		} 
	}
}

void cheng ()
{
	memset (c,0,sizeof(c));
	cl = 0;
	for (int i = 1; i <= al; i ++ )
	{
		for (int j = 1; j <= al; j++ )
		{
			cl++;
			c[cl].x = a[i].x + b[j].x;
			c[cl].y = a[i].y * b[j].y;
		}
	}
	sort (c + 1, c + 1 +cl, rule());
	for (int i = 1; i <= cl; i ++ )
	{
		if (c[i].x != d[dl].x)
		{
			dl ++ ;
			d[dl].x = c[i].x;
			d[dl].y += c[i].y;
		}
		else
		{
			d[dl].y += c[i].y;
		}
	}
}

int main ()
{
	cin >> al;
	create(a, al);
	cin >> bl;
	create(b, bl);
	cheng();
	print(d,dl);
	jia();
	print(c,cl);
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值