PAT 1009. Product of Polynomials (25)

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
#include <iostream>
#include <vector>
#include <iomanip>

class polynomial
{
private:
	std::vector<double> coefficient;
	std::vector<int> radix;
public:
	polynomial(){}
	polynomial(const polynomial &poly){ *this = poly; }
	~polynomial(){}
	friend polynomial operator+(const polynomial &poly1, const polynomial &poly2);
	friend polynomial operator-(const polynomial &poly1, const polynomial &poly2);
	polynomial& operator+=(const polynomial &poly){ *this = *this + poly; return *this; }
	polynomial& operator-=(const polynomial &poly){ *this = *this - poly; return *this; }
	friend polynomial operator*(const polynomial &poly1, const polynomial &poly2);
	friend std::istream& operator>>(std::istream &is, polynomial &poly);
	friend std::ostream& operator<<(std::ostream &os, const polynomial &poly);
};

polynomial operator+(const polynomial &poly1, const polynomial &poly2)
{
	polynomial polysum;

	int i = 0, j = 0;
	while (i<poly1.radix.size() || j<poly2.radix.size())
	{
		//only poly1
		if (i<poly1.radix.size() && j == poly2.radix.size())
		{
			polysum.radix.push_back(poly1.radix[i]);
			polysum.coefficient.push_back(poly1.coefficient[i]);
			i++;
		}
		//only poly2
		else if (j<poly2.radix.size() && i == poly1.radix.size())
		{
			polysum.radix.push_back(poly2.radix[j]);
			polysum.coefficient.push_back(poly2.coefficient[j]);
			j++;
		}
		//the radix of poly1 greater than poly2
		else if (poly1.radix[i] > poly2.radix[j])
		{
			polysum.radix.push_back(poly1.radix[i]);
			polysum.coefficient.push_back(poly1.coefficient[i]);
			i++;
		}
		//the radix of poly1 smaller than poly2
		else if (poly1.radix[i] < poly2.radix[j])
		{
			polysum.radix.push_back(poly2.radix[j]);
			polysum.coefficient.push_back(poly2.coefficient[j]);
			j++;
		}
		//the radix of poly1 equal to poly2
		else
		{
			if (poly1.coefficient[i] + poly2.coefficient[j] != 0)
			{
				polysum.radix.push_back(poly1.radix[i]);
				polysum.coefficient.push_back(poly1.coefficient[i] + poly2.coefficient[j]);
			}
			i++;
			j++;
		}
	}

	return polysum;
}

polynomial operator-(const polynomial &poly1, const polynomial &poly2)
{
	polynomial negativePoly;

	negativePoly = poly2;
	for (int i = 0; i < negativePoly.coefficient.size(); i++)
		negativePoly.coefficient[i] = -negativePoly.coefficient[i];

	return poly1 + negativePoly;
}

polynomial operator*(const polynomial &poly1, const polynomial &poly2)
{
	polynomial mul;

	int i = 0;
	while (i < poly2.coefficient.size())
	{
		polynomial part = poly1;
		double coefficient = poly2.coefficient[i];
		int radix = poly2.radix[i];

		for (int j = 0; j < part.coefficient.size(); j++)
		{
			part.radix[j] += radix;
			part.coefficient[j] *= coefficient;
		}
		mul += part;
		i++;
	}

	return mul;
}

std::istream& operator>>(std::istream &is, polynomial &poly)
{
	int k;
	is >> k;
	for (int i = 0; i<k; i++)
	{
		double tmpcoe;
		int tmprad;
		is >> tmprad >> tmpcoe;
		poly.radix.push_back(tmprad);
		poly.coefficient.push_back(tmpcoe);
	}

	return is;
}

std::ostream& operator<<(std::ostream &os, const polynomial &poly)
{
	os << poly.radix.size();
	if (poly.radix.size() != 0)
		std::cout << " ";
	for (int i = 0; i<poly.radix.size(); i++)
	{
		os << poly.radix[i] << " " << std::fixed << std::setprecision(1) << poly.coefficient[i];
		if (i != poly.radix.size() - 1)
			os << " ";
	}

	return os;
}

int main()
{
	polynomial poly1, poly2;

	std::cin >> poly1 >> poly2;

	std::cout << poly1*poly2;
}

  

时间: 2024-10-24 04:07:50

PAT 1009. Product of Polynomials (25)的相关文章

1009. Product of Polynomials (25)——PAT (Advanced Level) Practise

题目信息: 1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 c

1009 Product of Polynomials (25)(25 分)

1009 Product of Polynomials (25)(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 pol

1009 Product of Polynomials (25分) 多项式乘法

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 polynomi

[PTA] PAT(A) 1009 Product of Polynomials (25 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Result Problem portal:1009 Product of Polynomials Description Input Output Sample Sample Input Sample Output Solution Analysis Code #include <bits/stdc++.h>

PAT Advanced 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?2?? a?N?2?

PAT (Advanced Level) 1009. Product of Polynomials (25)

简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; double a[3000],b[3000],c[3000]; int k; int main() { for(int i=0;i<=2000;i++) a[i]=b[i]=c[i]=0; int Max=-1

PAT:1009. Product of Polynomials (25) AC

#include<stdio.h> #include<stdlib.h> #include<string.h> //[warning]double 输入%lf,输出%f struct arr { int exp; //指数 double cof; //系数 }arr[1005]; double ans[2010]; //下标是指数,内容是系数 int main() { memset(arr,0,sizeof(arr)); memset(ans,0,sizeof(ans)

PAT甲题题解-1009. Product of Polynomials (25)-多项式相乘

多项式相乘 注意相乘结果的多项式要开两倍的大小!!! #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string.h> using namespace std; //两个多项式相乘 const int maxn=1000+5; int k; //原本定义个exp导致编译错误,没想到定义成exp1.exp2也会编译错误,

1009 Product of Polynomials (25)(25 point(s))

problem 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 a~N1~ N2 a~N2~ ..