单链表应用之多项式加法

多项式加法

时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 1840            测试通过 : 204

描述

线性表是一种最简单、最基本,也是最常用的数据结构,其用途十分广泛,例如,用带表头结点的单链表求解一元整系数多项式加法和乘法运算。

现给两个一元整系数多项式,请求解两者之和。



输入

两组数据,每一组代表一个一元整系数多项式,有多行组成,其中每一行给出多项式每一项的系数和指数,这些行按指数递减次序排序,每一组结束行为

0 -1

输出

三组数据,前两组为一元整系数多项式,最后一组为两个多项式的和。

一元整系数多项式输出形式如下:

1)多项式项4x输出为4X

2)多项式项4x2输出为4X^2

3)第一项系数为正数时,加号不要输出

4)除常系数项外,项系数为1不显式输出,-1输出为-

例如,4x 3- x 2+x-1正确输出形式为4X^3-X^2+X-1,错误输出形式为 +4X^3-1X^2+1X-1

样例输入

3 14
-8 8
6 2
2 0
0 -1
2 10
4 8
-6 2
0 -1

样例输出

3X^14-8X^8+6X^2+2
2X^10+4X^8-6X^2
3X^14+2X^10-4X^8+2


这题我就偷懒了,因为对链表比较熟悉了,我就懒得写链表操作了,直接使用C++里面的list,这题的逻辑并不是很复杂,关键是细节问题,例如显示时,+1/-1,多项式开头,指数为0这些情况,都要考虑到。个人觉得难点就在这显示函数上,加法操作实在没什么好说的,就是指数相同的合并,不同的就把指数大的那个插入到结果list当中去,最后如果有余下的就直接拼接到结果list后面。


#include <iostream>
#include <list>
using namespace std;
typedef class item{
public:
	int xs;
	int zs;
	item(int xs,int zs){
		this->xs = xs;
		this->zs = zs;
	}
}ITEM;

typedef list<ITEM> LISTITEM;

/*
显示系数,+/-1是不显示那个1的
*/
void displayXS(int xs){
	if (xs == 1 || xs == -1) 
		return;
	else 
		cout << xs;
}

/*
显示指数,指数为0是不显示那个X的,指数为1显示X
*/
void displayZS(int zs){
	if (zs == 0)
		return;
	else if (zs == 1)
		cout << "X";
	else
		cout << "X^" << zs;
}

void display(LISTITEM &L){
	LISTITEM::iterator i = L.begin();
	if (i == L.end()) {
		cout << "0" << endl;
		return;
	}
	//开头是不需要显示+号的
	if (i->zs == 0&&(i->xs==-1||i->xs==1)){
		cout << i->xs;
	}
	else if (i->zs != 0 && i->xs == -1){
		cout << "-";
		displayZS(i->zs);
	}
	else{
		displayXS(i->xs);
		displayZS(i->zs);
	}
	i++;
	for (i; i != L.end(); i++){
		if (i->xs > 0){
			cout << "+";
		}
		if (i->zs == 0 && (i->xs == -1 || i->xs == 1)){
			cout << i->xs;
		}
		else if (i->zs != 0 && i->xs == -1){
			cout << "-";
			displayZS(i->zs);
		}
		else{
			displayXS(i->xs);
			displayZS(i->zs);
		}
	}
	cout << endl;
}

void add(LISTITEM &res, LISTITEM &h1, LISTITEM &h2){
	LISTITEM::iterator iter1, iter2;
	iter1 = h1.begin();
	iter2 = h2.begin();
	if (iter1 == h1.end() && iter2 == h2.end()){
		return;
	}
	for (; iter1 != h1.end() && iter2 != h2.end();){
		if (iter1->zs > iter2->zs){
			ITEM *temp = new ITEM(iter1->xs, iter1->zs);
			res.push_back(*temp);
			iter1++;
		}
		else if (iter1->zs < iter2->zs){
			ITEM *temp = new ITEM(iter2->xs, iter2->zs);
			res.push_back(*temp);
			iter2++;
		}
		else{
			if (iter1->xs + iter2->xs == 0){
				iter1++;
				iter2++;
			}
			else{
				ITEM *temp = new ITEM(iter1->xs+iter2->xs, iter1->zs);
				res.push_back(*temp);
				iter1++;
				iter2++;
			}
		}
	}
	if(iter1 == h1.end() && iter2!=h2.end())
	{
		for (; iter2 != h2.end(); iter2++){
			ITEM *temp = new ITEM(iter2->xs, iter2->zs);
			res.push_back(*temp);
		}
	}
	else if (iter2 == h2.end() && iter1 != h1.end())
	{
		for (; iter1 != h1.end(); iter1++){
			ITEM *temp = new ITEM(iter1->xs, iter1->zs);
			res.push_back(*temp);
		}
	}
}

int main(){
	LISTITEM head[2];
	int xs, zs;
	for (int i = 0; i < 2; i++){
		while (cin >> xs >> zs && (xs != 0 || zs != -1)){
			if (xs == 0) continue; //系数为零的就不加入list了
			ITEM *temp = new ITEM(xs,zs);
			head[i].push_back(*temp);
		}
	}
	LISTITEM res;
	add(res, head[0], head[1]);
	display(head[0]); display(head[1]); display(res);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值