#include<iostream>
#include<string.h>
const int num = 100;
//int a[num];//项数不等于数据长度
//int b[num];//数组在循环里定义好,还是采用其他方式比如new
//你的数组长度有问题,项数和数组长度没有统一
//没有考虑幂是负数的情况,凉凉
using namespace std;
void merge(int*a, int n, int*b, int m, int*c, int x);
void nerge(int*a,int n);
int main() {
int t;
cin>> t;
while (t--) {
int a[num], b[num], c[num];
memset(a, 0, sizeof(a));//没加头文件,不报错?
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
int n, m, p, q;
cin>> n;
for (int i = 0; i < n; i++ ) {
cin >> p >> q;
for (int i=0;i<num;i++)
{
if (q == i)
{
a[i] = p;
break;
}
}
}
n = q + 1;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> p >> q;
for (int i = 0; i<num; i++)
{
if (q == i)
{
b[i] = p;
break;
}
}
}
m = q + 1;
int x = n <= m ? n : m;
merge(a, n, b, m, c, x);
nerge(a, n);
nerge(b, m);
nerge(c, n >= m ? n : m);
}
return 0;
}
void merge(int*a, int n, int*b, int m, int*c,int x) {
for (int i = 0; i < x; i++) {
c[i] = a[i] + b[i];
}
if (n != m) {
if (n < m)
for (int i = x; i < m; i++)
c[i] = b[i];
else
for (int i = x; i < n; i++)
c[i] = a[i];
}
}
void nerge(int*a,int n) {
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
if (a[i] > 0) {
if (i == 0) cout << a[i] << " + ";
else if (i > 0 && i < n - 1) cout << a[i] << "x^" << i << " + ";
else cout << a[i] << "x^" << i << endl;
}
if (a[i] < 0) {
if (i == 0) cout << "(" << a[i] << ")" << " + ";
else if (i > 0 && i < n - 1) cout << "(" << a[i] << ")" << "x^" << i << " + ";
else cout << "(" << a[i] << ")" << "x^" << i << endl;
}
}
}
}
记一次刷题
猜你喜欢
转载自blog.csdn.net/Perce_Issac/article/details/82593962
今日推荐
周排行