PAT--1009 Product of Polynomials (25 分)

版权声明:未经过本人同意不得转发 https://blog.csdn.net/weixin_42956785/article/details/84728108

1009 Product of Polynomials (25 分)
This time, you are supposed to find A×B where A and B are two polynomials.

Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N​1,a​N​1,N​…Nk,aNk
where K is the number of nonzero terms in the polynomial, N
​i and aNi​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤NK<⋯<N2<N1<1000

Output Specification:
For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 3 3.6 2 6.0 1 1.6

译文

1009 多项式乘积 (25分)
这一次,你应该找到 A × B在哪里A和B是两个多项式。

输入规格:
每个输入文件包含一个测试用例。每个案例占用2行,每行包含多项式的信息:K N​1,a​N​1,N​…Nk,aNk
是指数和系数。给出了这一点1≤K≤10, 0≤NK<⋯<N2<N1<1000
输出规格:
对于每个测试用例,您应该输出产品 A和B在一行中,格式与输入相同。请注意,每行末尾必须没有额外的空间。请准确到小数点后1位。

样本输入:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
样本输出:
3 3 3.6 2 6.0 1 1.6

本想用vector数组来做这道题,但是它处理读入的double类型有问题,我就没有用了。这题最难的我个人认为最难的点在于他怎么知道它输出是几个数。

#include<cstdio>
#include<vector>
using namespace std;
double first[10000];
double second[10000];
double result[10000];
int main()
{
	int n;
	int max1, max2;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		int a;
		double b;
		scanf("%d%lf", &a, &b);
		if (i == 0)max1 = a;
		first[a] = b;
	}
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		int a;
		double b;
		scanf("%d%lf", &a, &b);
		if (i == 0)max2 = a;
		second[a] = b;
	}
	for (int i = 0; i <= max1; i++)
	{
		for (int j = 0; j <= max2; j++)
		{
			result[i + j] += (first[i] * second[j]);
		}
	}
	int max = max1 + max2;
	int Max = max+1;
	for (int i = max; i >= 0; i--)
	{
		if (result[i] == 0)Max--;
	}
	printf("%d", Max);
	for (int i = max; i >= 0; i--)
	{
		if (result[i] != 0)
		{
			printf(" %d %.1f", i, result[i]);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42956785/article/details/84728108