(祝某个今天脱单的好朋友999999~)1009 Product of Polynomials (25 分)

This time, you are supposed to find A×B where A and B are two polynomials.

这一次,你应该找到A×B,其中A和B是两个多项式。

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​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​ where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤N​K​​<⋯<N​2​​<N​1​​≤1000.

每个输入文件包含一个测试用例。每个案例占据2条线,每行包含多项式的信息:K N​1​​a​N​1​N​2​​a​N​2​.n​K​​a​N​K​,其中K是多项式中的非零项数,N​i和​aN​i​(i=1,2,⋯,K)分别是指数和系数。给出了1≤K≤10,0≤N​K​​...<N​2​​<N​1​​≤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.

对于每个测试用例,您应该在一行中输出A和B的乘积,格式与输入相同。请注意,每一行的末尾必须没有额外的空间。请精确到小数点1位。

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
#include<iostream>
#include<stdio.h>
using namespace std;
double b, arr[1001], ans[2001];//ans[指数]=系数, 最大为2000(a^1000*a^1000)
int n1, n2, a, cnt = 0;
int main()
{
	cin >> n1;//第一组中非零项的个数
	while (n1--)
	{
		cin >> a >> b;
		arr[a] = b;//存放系数的指数数组,arr[指数]=系数
	}
	cin >> n2;//第二组中非零项的个数
	while (n2--)
	{
		cin >> a >> b;
		for (int i = 0; i < 1001; i++)//到1000是因为arr数组最大为1000
			ans[i + a] += arr[i] * b;//arr数组中的每一个数都要乘第二组数据的系数
		//[j+a]表示相乘代表的指数相加,‘+=’表示同指数的系数相加
	}
	for (int i = 0; i < 2001; i++)//遍历ans数组要到2000
		if (ans[i])cnt++;//ans[指数]=系数,统计系数不为0的个数计非零项
	cout << cnt;
	for (int i = 2000; i >= 0; i--)
		if (ans[i])
			printf(" %d %.1f", i, ans[i]);
}

猜你喜欢

转载自blog.csdn.net/qq_43813697/article/details/89788397
今日推荐