#include <iostream>
#include <cmath>
using namespace std;
#define MAX 2000
#define EPS 1e-6
double res[MAX][MAX] = {0};
int max_a_index = 0, max_b_index = 0;//分别记录多项式的最高次数
void input(int N, double (&a)[MAX], int &index);
void output(int N1, double (&a)[MAX], int N2, double (&b)[MAX]);
int main() {
double a[MAX] = {0}, b[MAX] = {0};
int K1, K2;
scanf("%d", &K1);
input(K1, a, max_a_index);
scanf("%d", &K2);
input(K2, b, max_b_index);
output(K1, a, K2, b);
}
void input(int N, double (&arr)[MAX], int &max_index) {
int exp = 0;
double coef = 0;
for (int i = 0; i < N; ++i) {
scanf("%d %lf", &exp, &coef);
arr[exp] = coef;//指数exp对应的系数是coef
if (max_index < exp) {//传入max_a_index,则记录a多项式的最高的次数,如果传入的是max_b_index,则记录的是多项式b的最高次数
max_index = exp;
}
}
}
/*
2 1 2.4 0 3.2
2 2 1.5 1 0.5
*/
/*
3 1 1 2 1 3 1
3 1 1 3 -1 5 1
*/
void output(int N1, double (&a)[MAX], int N2, double (&b)[MAX]) {
int i = 0, j = 0, count = 0;
double sum = 0;
//负责暂时存储左多项式的一个系数乘以右多项式的结果,每一行代表一个结果,m*n个结果存储到一个m*n的二维数组里面去
for (i = 0; i <= max_a_index; ++i) {
for (j = 0; j <= max_b_index; ++j) {
if (abs(a[i]) > EPS && abs(b[j]) > EPS) {
res[i][i + j] = a[i] * b[j];
}
}
}
//自上而下负责把对应系数相加,把m个多项式的结果相加到一起
for (j = 0; j <= max_b_index + max_a_index; ++j) {//max_b_index=5
for (i = 0; i <= max_a_index; ++i) {//max_a_index=3
sum += res[i][j];
}
res[0][j] = sum;
sum = 0;
}
//统计非零项个数
for (i = 0; i <= max_a_index + max_b_index; ++i) {
if (abs(res[0][i]) > EPS) {
++count;
}
}
printf("%d", count);
//从尾到头输出多项式的结果
for (i = max_b_index + max_a_index; i >= 0; --i) {
if (abs(res[0][i]) > EPS) {
printf(" %d %.1f", i, res[0][i]);
}
}
}
07-15
798
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-19
214
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-17
282
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交