模拟多项式乘法
输入样例:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
输出样例:
3 3 3.6 2 6.0 1 1.6
思路分析
模拟多项式相乘,固定一个,和后面所有的相乘
用一个结构体数组(或正常数组..)存储系数和幂
两项相乘幂相加,对应结构体数组中系数相乘
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int N = 10010;
struct Pol
{
int mi;
double xi;
} p[N], q[N],res[N];
int main()
{
int k1 , k2,n;
double a;
scanf("%d",&k1 );
for ( int i =0; i < k1; i++ ) {
scanf("%d %lf", &n, &a);
p[i].mi = n;
p[i].xi = a;
}
scanf("%d",&k2 );
for ( int i=0; i < k2; i++ ) {
scanf("%d %lf", &n, &a);
q[i].mi = n;
q[i].xi = a;
}
int cnt = 0;
// important!!
for ( int i=0; i < k1; i++ ) {
for ( int j=0; j < k2; j++ ) {
res[ p[i].mi + q[j].mi ].mi = p[i].mi + q[j].mi;
res[ p[i].mi + q[j].mi ].xi += p[i].xi * q[j].xi;
}
}
for ( int i=N; i>=0; i-- )
if ( int(res[i].xi) != 0 ) cnt++;
cout << cnt;
for ( int i= N; i>=0; i-- ) {
if ( int(res[i].xi) != 0 ) {
printf(" %d %.1f", i, res[i].xi);
}
}
return 0;
}