PATA 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 N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni 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

题意:与PATA1002基本相同,只是从加法变成了乘法,输入输出的格式也与1002相同

AC代码

#include<iostream>
#include<stdio.h>
using namespace std;
struct poly
{
	int exp;
	double coe;
}poly[1005];//结构体的运用很巧妙 
double result[2005]={0};
int main()
{
	int k,m;
	cin>>k;
	for(int i=0;i<k;i++)
	cin>>poly[i].exp>>poly[i].coe;
	cin>>m;
	for(int i=0;i<m;i++)//依旧与加法的做法类似,用指数做下标,将计算得出的系数放入数组中 
	{
		int exp1;
		double coe1;
		cin>>exp1>>coe1;
		for(int j=0;j<k;j++)
		result[poly[j].exp+exp1]+=(poly[j].coe*coe1);
	}
	int count=0;
	for(int i=2004;i>=0;i--)//因为是乘法,所以最大的指数应该是2000 
	{
		if(result[i]!=0)
		count++;
	}
	cout<<count;
	for(int i=2004;i>=0;i--)
	{
		if(result[i]!=0.0)
		printf(" %d %.1f",i,result[i]);
	}
	return 0;
}
总结:结构体的运用很巧妙,将指数和系数捆绑在一起,其余和1002类似

猜你喜欢

转载自blog.csdn.net/qq_33657357/article/details/80300000