代码参考胡凡笔记
#pragma warning(disable:4996)
#include<cstdio>
#include<vector>
using namespace std;
/*
总结:这道题关键在于多项式乘积的理解
多项式相乘的结果,指数是两者指数相加,系数相乘
注意点:
1)答案的系数数组要够大,起码大于2000
2)第二个数组不用保存,可以边读边边处理
*/
struct Poly {
int exp;
double value;
}poly[1001];
double ans[2001];
int main() {
int n, m, number = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %lf", &poly[i].exp, &poly[i].value);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int exp;
double value;
scanf("%d %lf", &exp, &value);
for (int j = 0; j < n; j++) {
ans[exp + poly[j].exp] += (value*poly[j].value);
}
}
for (int i = 0; i <= 2000; i++) {
if (ans[i] != 0.0)number++;
}
printf("%d", number);
for (int i = 2000; i >= 0; i--) {
if (ans[i] != 0.0) {
printf(" %d %.1f", i, ans[i]);
}
}
system("pause");
return 0;
}
本文深入探讨了多项式乘积算法的实现细节,重点讲解了指数相加和系数相乘的原则,以及如何通过合理设置数组大小来避免溢出,确保计算准确性。文章通过具体代码示例,展示了如何读取两个多项式并计算它们的乘积,最后输出结果。
371

被折叠的 条评论
为什么被折叠?



