POJ 多项式加法

该博客探讨了如何使用程序模拟两个多项式的相加。每个多项式由系数和指数对表示,如2x^20 - x^17 + 5x^9 - 7x^7 + 16x^5等。输入的指数顺序可能是随机的,且每个多项式以负指数对结束。任务是将两个这样的多项式正确地相加在一起。
摘要由CSDN通过智能技术生成
总时间限制: 
1000ms 
内存限制: 
5000kB
描述

    我们经常遇到两多项式相加的情况,在这里,我们就需要用程序来模拟

实现把两个多项式相加到一起。首先,我们会有两个多项式,每个多项式是

独立的一行,每个多项式由系数、幂数这样的多个整数对来表示。

如多项式2x20- x17+ 5x9- 7x7+ 16x5+ 10x+ 22x2- 15

对应的表达式为:2 20 -1 17 5 9 - 7 7 16 5 10 4 22 2 -15 0。  

为了标记每行多项式的结束,在表达式

后面加上了一个幂数为负数的整数对。

同时输入表达式的幂数大小顺序是随机的。

我们需要做的就是把所给的两个多项式加起来。

输入
输入包括多行。第一行整数n,表示有多少组的多项式需要求和。(1<n<100)下面为2n行整数,每一行都是一个多项式的表达式。表示n组需要相加的多项式。每行长度小于100。
输出
输出包括n行,每行为1组多项式相加的结果。在每一行的输出结果中,多项式的每一项用“[x y]”形式的字符串表示,x是该项的系数、y 是该项的幂数。要求按照每一项的幂从高到低排列,即先输出幂数高的项、再输出幂数低的项。系数为零的项不要输出。
样例输入
2
-1 17 2 20 5 9 -7 7 10 4 22 2 -15 0 16 5 0 -1
2 19 7 7 3 17 4 4 15 10 -10 5 13 2 -7 0 8 -8
-1 17 2 23 22 2 6 8 -4 7 -18 0 1 5 21 4 0 -1
12 7 -7 5 3 17 23 4 15 10 -10 5 13 5 2 19 9 -7
样例输出
[ 2 20 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 5 9 ] [ 6 5 ] [ 14 4 ] [ 35 2 ] [ -22 0 ]
[ 2 23 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 6 8 ] [ 8 7 ] [ -3 5 ] [ 44 4 ] [ 22 2 ] [ -18 0 ]
提示

第一组样例数据的第二行末尾的8 -8,因为幂次-8为负数,所以这一行数据结束,8 -8不要参与计算。



#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <utility>
#include <sstream>

void getTwoPolynomial(std::vector<std::pair<int, int> > &lhs,
				 std::vector<std::pair<int, int> > &rhs)
{
	int coefficient, power;
	std::string strBuf;
	std::stringstream ss;

	// get first polynomial
	std::getline(std::cin, strBuf);
	ss.str(strBuf);
	while (!ss.eof())
	{
		ss >> coefficient >> power;
		if (0 > power)
		{
		    break;
		}

		lhs.push_back(std::make_pair(coefficient, power));
	}

	// get second polynomial
	std::getline(std::cin, strBuf);
	ss.str("");
	ss.clear();
	ss.str(strBuf);
	while (!ss.eof())
	{
	    ss >> coefficient >> power;
		if (0 > power)
		{
		    break;
		}

		rhs.push_back(std::make_pair(coefficient, power));
	}
}

struct descend_order : public std::unary_function<std::pair<int, int>,
	bool>
{
	result_type operator()(const argument_type &lhs, const argument_type &rhs)
	{
		return lhs.second > rhs.second;
	}
};

void compute(std::vector<std::pair<int, int> > &ret,
			 std::vector<std::pair<int, int> > &lhs,
			 std::vector<std::pair<int, int> > &rhs)
{
	// sort in descending order
	std::sort(lhs.begin(), lhs.end(), descend_order());
	std::sort(rhs.begin(), rhs.end(), descend_order());

	std::pair<int, int> tmp;
	std::vector<std::pair<int, int> >::iterator next;
	std::vector<std::pair<int, int> >::iterator itl = lhs.begin();
	std::vector<std::pair<int, int> >::iterator itr = rhs.begin();
	while (lhs.end() != itl && rhs.end() != itr)
	{
		if (itl->second > itr->second)
		{
			tmp  = *itl;
			next = itl;
			++next;
			// check the power of the next item is the same 
			while (lhs.end() != next && next->second == tmp.second)
			{
				tmp.first += next->first;
				++next;
			}
			itl = next;
		}
		else if (itl->second < itr->second)
		{
			tmp  = *itr;
			next = itr;
			++next;
			// check the power of the next item is the same 
			while (rhs.end() != next && next->second == tmp.second)
			{
				tmp.first += next->first;
				++next;
			}
			itr = next;
		}
		else
		{
			tmp.second = itl->second;
			tmp.first  = itl->first+itr->first;
			next       = itl;
			++next;
			// check the power of the next item is the same 
			while (lhs.end() != next && next->second == tmp.second)
			{
				tmp.first += next->first;
				++next;
			}
			itl  = next;

			next = itr;
			++next;
			// check the power of the next item is the same 
			while (rhs.end() != next && next->second == tmp.second)
			{
				tmp.first += next->first;
				++next;
			}
			itr = next;
		}

		if (0 == tmp.first)
		{
			continue;
		}
		ret.push_back(tmp);
	}

	while (lhs.end() != itl)
	{
		tmp  = *itl;
		next = itl;
		++next;
		// check the power of the next item is the same 
		while (lhs.end() != next && next->second == tmp.second)
		{
			tmp.first += next->first;
			++next;
		}
		itl = next;

		if (0 != tmp.first)
		{
			ret.push_back(tmp);
		}
	}

	while (rhs.end() != itr)
	{
		tmp  = *itr;
		next = itr;
		++next;
		// check the power of the next item is the same 
		while (rhs.end() != next && next->second == tmp.second)
		{
			tmp.first += next->first;
			++next;
		}
		itr = next;

		if (0 != tmp.first)
		{
			ret.push_back(tmp);
		}
	}
}

void print(std::vector<std::pair<int, int> > &ret)
{
	std::vector<std::pair<int, int> >::iterator it;

	for (it=ret.begin(); it!=ret.end(); ++it)
	{
		std::cout << "[ " << it->first << ' ' << it->second << " ] "; 
	}

	std::cout << '\n';
}

int main(void)
{
	int n;
	std::vector<std::pair<int, int> > lhs;
	std::vector<std::pair<int, int> > rhs;
	std::vector<std::pair<int, int> > ret;
	
	std::cin >> n;
	std::cin.ignore(2, '\n');
	lhs.reserve(50);
	rhs.reserve(50);
	ret.reserve(50);

	while (0 < n)
	{
		getTwoPolynomial(lhs, rhs);
		compute(ret, lhs, rhs);
		print(ret);

		--n;
		lhs.clear();
		rhs.clear();
		ret.clear();
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值