#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);
}
7-2 一元多项式的乘法与加法运算 (20 分)
于 2022-03-29 10:09:12 首次发布