-
设计函数分别求两个一元多项式的乘积与和。
-
输入格式:
输入分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 <bits/stdc++.h>
using namespace std;
typedef struct dec *LinkList;
struct dec
{
int coef; //系数
int expon; //指数
LinkList next;
};
LinkList Read();
void Attach(int c, int e, LinkList *head, LinkList *tail);
void Insert(int c, int e, LinkList *head, LinkList *tail);
void print(LinkList L);
void Renew(LinkList *head);
LinkList Add(LinkList L1, LinkList L2);
LinkList Mul(LinkList L1, LinkList L2);
int main()
{
LinkList L1, L2, add, mul;
L1 = Read();
L2 = Read();
add = Add(L1, L2);
mul = Mul(L1, L2);
print(mul);
cout << endl;
print(add);
return 0;
}
void Renew(LinkList *head)
{
while (*head && (*head)->coef == 0)
*head = (*head)->next;
if (*head == NULL)
return;
LinkList front = *head, tail = front->next;
while (tail)
{
if (tail->coef == 0)
{
front->next = tail->next;
free(tail);
}
front = front->next;
tail = front->next;
}
}
LinkList Mul(LinkList L1, LinkList L2)
{
if (L1 == NULL || L2 == NULL)
return NULL;
LinkList head = NULL, tail = NULL;
LinkList point2 = L2, point1 = L1;
int c, e;
while (point2) //先将L1的一个元素和L2上的每一个元素相乘,构成一条链表
{
c = (point1->coef) * (point2->coef);
e = (point1->expon) + (point2->expon);
Attach(c, e, &head, &tail);
point2 = point2->next;
}
point1 = point1->next; //这一步不能漏,第一条链表的第一个节点已与第二条链表每一个节点相乘,所以第一条链表的节点要后移
while (point1)
{
point2 = L2;
while (point2) //链表一的每一个节点与链表二的每一个节点相乘插入已经构成的链表
{
int c = point1->coef * point2->coef;
int e = point1->expon + point2->expon;
Insert(c, e, &head, &tail);
point2 = point2->next;
}
point1 = point1->next;
}
Renew(&head);
return head;
}
void Insert(int c, int e, LinkList *head, LinkList *tail)
{
LinkList point = *head;
if (e > point->expon)
{
LinkList temp = (LinkList)malloc(sizeof(struct dec));
temp->expon = e;
temp->coef = c;
temp->next = *head;
*head = temp;
return;
}
else if (e < (*tail)->expon) //尾部插入
{
Attach(c, e, &*head, &*tail);
return;
}
while (point) //不能用head,会改变head的指向的,因为传入Insert函数的是head的二级指针
{
if (point->expon == e)
{
point->coef += c; //指数相同,系数相加
return;
}
else if (e < point->expon && e > (point->next)->expon) //中间插入
{
LinkList temp = (LinkList)malloc(sizeof(struct dec));
temp->expon = e;
temp->coef = c;
temp->next = point->next;
point->next = temp;
return;
}
point = point->next;
}
}
LinkList Add(LinkList L1, LinkList L2)
{
if (L1 == NULL && L2 == NULL)
return NULL; //***这个没加导致第四个点一直过不去
LinkList head = NULL, tail = NULL;
int c, e;
while (L1 && L2)
{
if (L1->expon == L2->expon) //指数相同,系数相加
{
c = L1->coef + L2->coef;
e = L1->expon;
if (c)
Attach(c, e, &head, &tail); //系数不为0才保存
L1 = L1->next, L2 = L2->next;
}
else if (L1->expon > L2->expon)
{
if (L1->coef)
Attach(L1->coef, L1->expon, &head, &tail);
L1 = L1->next;
}
else
{
if (L2->coef)
Attach(L2->coef, L2->expon, &head, &tail);
L2 = L2->next;
}
}
while (L1)
{
if (L1->coef)
Attach(L1->coef, L1->expon, &head, &tail);
L1 = L1->next;
}
while (L2)
{
if (L2->coef)
Attach(L2->coef, L2->expon, &head, &tail);
L2 = L2->next;
}
return head;
}
LinkList Read()
{
LinkList head = NULL, tail = NULL;
int n, c, e;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> c >> e;
Attach(c, e, &head, &tail);
}
return head;
}
void Attach(int c, int e, LinkList *head, LinkList *tail)
{
LinkList temp = (LinkList)malloc(sizeof(struct dec));
temp->next = NULL;
temp->coef = c;
temp->expon = e;
if (*head == NULL)
*head = temp;
else
(*tail)->next = temp;
*tail = temp;
}
void print(LinkList L)
{
if (L == NULL)
{
cout << 0 << " " << 0;
return;
}
int flag = 0;
while (L)
{
if (flag != 0)
cout << " ";
flag = 1;
cout << L->coef << " " << L->expon;
L = L->next;
}
}