1. 程式人生 > >1009. Product of Polynomials (25)-PAT甲級真題

1009. Product of Polynomials (25)-PAT甲級真題

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int n1, n2,e;
	double c, arr[1001] = { 0.0 }, ans[2001] = { 0.0 };
	cin >> n1;
	for (int i = 0; i < n1; i++)
	{
		cin >> e >> c;
		arr[e] = c;
	}

	cin >> n2;
	for (int i = 0; i < n2; i++)
	{
		cin >> e >> c;
		
		for (int j = 0; j < 1001; j++)
		{
			ans[e + j] += arr[j] * c;
		}
	}

	int cnt = 0;
	for (int i = 0; i < 2001; i++)
	{
		if (ans[i]!=0.0)
		{
			cnt++;
		}
	}
	cout << cnt;
	for (int i = 2000; i >=0; i--) //此處指數有可能等0(常數項)  so i要取到到0
	{
		if (ans[i]!=0.0)
		{
			cout <<" "<< i << " "
				<< fixed<<setprecision(1)<< ans[i];
		}
		
	}
	return 0;
}