Problem Description
让我们重温小学初中时那些一元多项式的加法吧,不同的是现在使用计算机来帮我们计算了。多年前不想写作业的梦想终于快要实现了!下面就给出书上的算法。
图:链表实现一元多项式加法
Input Description
输入数据包含多组测试数据,每组数据包含两行一元多项式。每个多项式包含若干对整数,每对整数的第一个是系数,第二个是指数。每个多项式不超过100项,整数间用空格隔开,并且指数是递减的。
Output Description
每组测试数据输出一行结果,每个整数后面空一格。(包括行尾)
Sample Input
3 2 4 1 7 0 2 4 1 1 2 3 1 4 3 2 4 1 7 0 2 4 -4 1
Sample Output
2 4 3 2 5 1 7 0 1 4 2 3 2 4 3 2 7 0
Hint
提示:
1、由于多项式元素的重要信息在系数和指数,所以可以定义结点类型为含两个整数的结构体,一个代表系数而另一个代表指数。 2、定义完数据类型后,主要的就是怎么读取数据了。由于每个多项式占一行,所以可以用gets来读取一行,并判断是否为空行:while(gets(strA) && strlen(strA))...然后就将字符串中的数据转换为多项式类型。此时使用到一个字符串处理函数char * strtok ( char * str, const char * delimiters )。这个函数的主要功能是将字符串str按delimiters中的字符分割。使用这个字符串处理函数时注意在处理某个字符串时首次使用时传递的参数是字符串指针而以后在使用时传递的参数是NULL。 3、下面的算法与有序序列有序合并算法类似。因为是多次循环,如果里面含有迭代变量(i,j之类的)注意下次循环时初值对不对。
总结:
多项式加法的算法与有序序列有序合并的算法类似,注意多项式元素类型的定义即可。
#include <iostream>
#include <cstring>
using namespace std;
typedef struct PLink
{
double coef;//系数
int expn;//指数
struct PLink *next;
}PLink, *PLinkList;
void Cushihua(PLinkList &p)//初始化
{
p = (PLinkList)malloc(sizeof(PLink));
p->coef = 0;
p->expn = 0;
p->next = NULL;
}
//存入数据
void InsterPLinkList(PLinkList &l, int m, int n)
{
PLinkList p, q;
p = l;
q = (PLinkList)malloc(sizeof(PLink));
q->coef = m;
q->expn = n;
q->next = NULL;
while (p->next != NULL)
{
p = p->next;
}
q->next = p->next;
p->next = q;
}
void print(PLinkList &l)
{
PLinkList p;
p = l->next;
int i = 0;
while (p != NULL)
{
if (i++ > 0)
{
cout << " ";
}
cout << p->coef << " " << p->expn << " ";
p = p->next;
}
cout << endl;
}
void AddLinkList(PLinkList &pa, PLinkList &pb)
{
PLinkList p1, p2, p3, r;
p1 = pa->next;
p2 = pb->next;
p3 = pa;
int sum;
while (p1 && p2)
{
if (p1->expn == p2->expn)//p1指数和p2相等
{
sum = p1->coef + p2->coef;
if (sum != 0)
{
p1->coef = sum;
p3->next = p1;
p3 = p1;
p1 = p1->next;
r = p2;
p2 = p2->next;
delete r;
}
else
{
r = p1;
p1 = p1->next;
delete r;
r = p2;
p2 = p2->next;
delete r;
}
}
else if (p1->expn < p2->expn)
{
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
else
{
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
}
p3->next = p1 ? p1 : p2;
delete pb;
}
void Print(PLinkList &l)
{
PLinkList p;
p = l->next;
while (p != NULL)
{
cout << p->coef << " " << p->expn << " ";
p = p->next;
}
cout << endl;
}
int main()
{
char a[200], b[200];
while (gets_s(a))
{
int c[100], d[100];
gets_s(b);
int leng1 = 0;
int leng2 = 0;
int leng3 = 0;
int leng4 = 0;
leng1 = strlen(a);
leng2 = strlen(b);
for (int i = 0; i < leng1; i++)
{
if (a[i] != ' ')
{
if (a[i] != '-')
{
c[leng3] = a[i] - '0';
leng3++;
continue;
}
else if(a[i] == '-')
{
i++;
c[leng3] = -(a[i] - '0');
leng3++;
}
}
}
for (int i = 0; i < leng2; i++)
{
if (b[i] != ' ')
{
if (b[i] != '-')
{
d[leng4] = b[i] - '0';
leng4++;
continue;
}
else if (b[i] == '-')
{
i++;
d[leng4] = -(b[i] - '0');
leng4++;
}
}
}
PLinkList pa, pb;
Cushihua(pa);
Cushihua(pb);
int m, n;
for (int j = 0; j < leng3; j++)
{
m = c[j];
j++;
n = c[j];
InsterPLinkList(pa, m, n);
}
for (int j = 0; j < leng4; j++)
{
m = d[j];
j++;
n = d[j];
InsterPLinkList(pb, m, n);
}
AddLinkList(pa, pb);
Print(pa);
}
return 0;
}
单链表C语言版
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct sl {
int da;
int dp;
sl* next;
};
sl* head = new sl;
void sd(char* sa);
void insert(sl* np);
void slprin();
void slprin();
int main() {
char sa[1000];
char sb[1000];
while (gets(sa) && strlen(sa) && gets(sb) && strlen(sb)) {
head->next = NULL;
sd(sa);
sd(sb);
slprin();
}
}
void sd(char* sa) {
for (int i = 0; i < strlen(sa)-2; i+=4) {
int da = 0, dd = 0;
if (sa[i] == '-') {
i++;
da = 1;
}
int c = sa[i] - '0';
if (sa[i + 2] == '-') {
i++;
dd = 1;
}
int d = sa[i+2] - '0';
if (da == 1) c = -c;
if (dd == 1) d = -d;
sl* np = new sl;
np->da = c;
np->dp = d;
np->next=NULL;
insert(np);
}
}
void insert(sl* np) {
sl* p = head;
for (; p->next && p->next->dp > np->dp; p = p->next);
if (p->next&&p->next->dp == np->dp) {
p->next->da += np->da;
delete np;
}
else {
np->next = p->next;
p->next = np;
}
}
void slprin() {
sl* p = head->next;
for (; p; p = p->next) {
if (p->da != 0) {
cout << p->da << " " << p->dp << " ";
}
}
cout << endl;
}