单链表应用之多项式加法
2013-05-01 21:52
267 查看
多项式加法
时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte总提交 : 1840 测试通过 : 204
描述
线性表是一种最简单、最基本,也是最常用的数据结构,其用途十分广泛,例如,用带表头结点的单链表求解一元整系数多项式加法和乘法运算。
现给两个一元整系数多项式,请求解两者之和。
输入
两组数据,每一组代表一个一元整系数多项式,有多行组成,其中每一行给出多项式每一项的系数和指数,这些行按指数递减次序排序,每一组结束行为
0 -1
输出
三组数据,前两组为一元整系数多项式,最后一组为两个多项式的和。
一元整系数多项式输出形式如下:
(1)多项式项4x输出为4X
(2)多项式项4x2输出为4X^2
(3)第一项系数为正数时,加号不要输出
(4)除常系数项外,项系数为1不显式输出,-1输出为-
例如,4x3- x2+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; }
相关文章推荐
- 链表应用--多项式加法、乘法
- 链表的应用 —— 多项式运算(加法+乘法)
- 链表的应用:单元多项式的加法、减法、乘法
- 链表的应用:单元多项式的加法、减法、乘法
- (1.2.1.5)链表的应用:单元多项式的加法、减法、乘法
- 用单向链表实现一元多项式加法 推荐
- 链表实现一元多项式加法
- 链表的多项式加法链表的多项式加法
- 多项式加法,链表方法实现
- 链表在计算多项式上的应用(1)
- C/C++(链表应用)笔记——使用链表进行多项式的合并
- 链表的应用---多项式运算
- 链表实现多项式的加法和乘法
- 多项式加法利用链表实现(c语言)
- 单链表的应用——多项式加法的C语言实现(链式存储结构)
- 链表之一元多项式加法
- 链表在多项式的应用(2)
- 多项式加法(单链表 c语言)
- 线性表的应用——求解两个多项式相加问题描述(链表求解)
- 一元多项式相乘 - 链表的简单应用