习题3.6 一元多项式的乘法与加法运算 (20point(s))
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
这鬼题目用链表写看着就头大,所以怎么好写就怎么写了,加法的代码可以优化。
乘法设计逐项相乘降序插入
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
const int MAXSIZE = 100000 + 10;
typedef int ElementType;
typedef struct LList* PtrToLList;
struct LList {
ElementType Data;
ElementType index;
PtrToLList next;
};
typedef PtrToLList Position;
typedef PtrToLList List;
//尾插法创建链表void CreatList(List &L,int n)
List TailCreatList(int n)
{
List L = (List)malloc(sizeof(struct LList));
L->next = NULL;
List temp = L;
for (int i = 0; i < n; ++i)
{
List p = (List)malloc(sizeof(struct LList));
p->next = NULL;
scanf("%d%d", &p->Data, &p->index);
temp->next = p;
temp = p;
}
return L;
}
int Length(List L)
{
int length = 0;
Position p = L;
while (p)
{
p = p->next;
++length;
}
return length;
}
void show(List p)
{
p = p->next;
if (!p)
{
printf("0 0\n");
return;
}
int flag = 0;
while (p)
{
if (!p->Data && !p->index)
{
}
else if (!p->Data && p->index)
{
printf("0 0");
}
else
{
if (!flag)
flag = 1;
else
printf(" ");
printf("%d %d", p->Data, p->index);
}
p = p->next;
}
printf("\n");
}
/*
带头结点的插入,结点之前插入新结点
int Insert(List L, ElementType X, Position P)
*/
int Insert(List L, ElementType X, ElementType Y, int i)
{
Position temp, pre;
int cnt = 0;
for (pre = L; pre && cnt < i - 1; pre = pre->next)
cnt++;
if (pre == NULL || cnt != i - 1)
{
printf("插入位置参数错误\n");
return 0;
}
else
{
temp = (Position)malloc(sizeof(struct LList));
temp->Data = X;
temp->index = Y;
temp->next = pre->next;
pre->next = temp;
return 1;
}
}
/*
在已经降序排列好的链表中插入到指数刚好比aid值大的前面位置
如果有指数重复,就系数相加
int push(List L, ElementType X, ElementType Y)
*/
int push(List L, ElementType X, ElementType Y)
{
Position pre=L,temp;
while (pre->next && pre->next->index > Y)
pre = pre->next;
if (pre->next && pre->next->index == Y) {
if (pre->next->Data + X)
{
pre->next->Data += X;
return 1;
}
else
{
List t = pre->next;
pre->next = t->next;
free(t);
return 1;
}
}
else
{
temp = (Position)malloc(sizeof(struct LList));
temp->Data = X;
temp->index = Y;
temp->next = pre->next;
pre->next = temp;
return 1;
}
}
//链表加法
List add(List L1, List L2)
{
List p = (List)malloc(sizeof(struct LList));
p->next = NULL;
int i = 1;
L1 = L1->next, L2 = L2->next;
while (L1 || L2)
{
if (L1 && L2)
{
if (L1->index > L2->index)
{
Insert(p, L1->Data, L1->index, i);
L1 = L1->next;
}
else if (L2->index > L1->index)
{
Insert(p, L2->Data, L2->index, i);
L2 = L2->next;
}
else if (L1->index == L2->index)
{
Insert(p, L1->Data + L2->Data, L1->index, i);
L1 = L1->next;
L2 = L2->next;
}
}
else if (L1 && !L2)
{
Insert(p, L1->Data, L1->index, i);
L1 = L1->next;
}
else if (!L1 && L2)
{
Insert(p, L2->Data, L2->index, i);
L2 = L2->next;
}
i++;
}
return p;
}
List multiply(List L1, List L2)
{
if (!L1 || !L2)
{
printf("空数据");
return NULL;
}
List p = (List)malloc(sizeof(struct LList));
p->next = NULL;
L1 = L1->next, L2 = L2->next;
for (List i = L1; i; i = i->next)
for (List j = L2; j; j = j->next)
push(p, i->Data * j->Data, i->index + j->index);
return p;
}
int main()
{
List list1, list2;
int n = 0;
scanf("%d", &n);
list1 = TailCreatList(n);
scanf("%d", &n);
list2 = TailCreatList(n);
show(multiply(list1, list2));
show(add(list1, list2));
}
/*
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
2 1 2 1 0
2 1 2 -1 0
2 -1000 1000 1000 0
2 1000 1000 -1000 0
0
1 999 1000
*/