1009 Product of Polynomials (25 分)

1009 Product of Polynomials (25 分)

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=10000;
int main() {
	//数组a,b用来接收指数和系数,
	//数组c用来接收a和b相乘的指数和系数 
	double a[maxn], b[maxn], c[maxn];
	int k;
	memset(c,0,sizeof(double)*maxn);
	int expo ;
	double coef;
	cin >> k;
	int max1 = 0;
	for (int i = 0; i < k; i++) {
		cin >> expo >> coef;
		if (max1 < expo) 
			max1 = expo;
		a[expo] = coef;
	}
	int n;
	cin>>n;
	int max2 = 0;
	for (int i = 0; i < n; i++) {
		cin >> expo >> coef;
		if (max2 < expo)
			max2 = expo;
		b[expo] = coef;
	}
	int count = 0;
	for (int i = 0; i <= max1; i++) {
		if (a[i] == 0.0) continue;
		for (int j = 0; j <= max2; j++) {
			if (b[j] == 0.0) continue;
			
			c[i+j] += a[i] * b[j];
//			printf("a[%d]=%.1f b[%d]=%.1f\n",i,a[i],j,b[j]);  
		}
	}
	for (int i = 0; i < maxn; i++) {
		if (c[i] != 0.0) {
			count++;
		}
	}
	cout<<count;
	for (int i = maxn-1; i >= 0; i--) {
		if (c[i] != 0.0)
			printf(" %d %.1f",i,c[i]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37131037/article/details/83316258