This time, you are supposed to find A*B where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < …< N2 < N1 <=1000.
Output Specification:
For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.
翻译:
这一次,你要做的是找到A和B这两个多项式的乘积.
输入格式:
每个输入文件都包含一个测试用例。 每一行占用2行,每一行包含多项式的信息:其中K是多项式中非零项的个数,Ni和aNi(i = 1,2,…,N)。 …,K)分别是指数和系数。 其中1 <= K <= 10, 0 <= NK < …< N2 < N1 <=1000.
输出格式:
对于每个测试用例,你应该在一行中输出A和B的乘积,格式与输入相同。 请注意,每行末尾不得有额外的空格。 精确到小数点后1位。
思路:
使用链表来表示多项式A,B
步骤:
- 使用两次循环,将B的每一项与A的每一项相乘,得到链表P
- 多项式降幂排列: 遍历P的指数成员,将其存在一个数组中,然后对这个数组排序(降序),再依次在链表中查找数组中每一个元素,把每一个节点复制到新的链表中.
- 合并同类项: 在上一步中已经把多项式降幂排列了,所以如果某个节点的指数成员和下一个节点的指数成员相等,就可以把它们合并成一项.
- 删除系数为零的项:遍历链表,如果某个节点的系数成员为零,则删除这一项.
代码(我的环境为ms vs2017):
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <time.h>
#include <iomanip>
#include <math.h>
using namespace std;
struct LinkedList
{
int Exp;//指数
double Coe;//系数
LinkedList* Next;
};
typedef LinkedList* List;
int cmp(const void* a, const void* b)
{
return *(int*)a - *(int*)b;
}
class Stack
{
private:
int NodeNumbers;
List L;
public:
Stack()
{
NodeNumbers = 0;
L = new LinkedList;
L->Coe = L->Exp = 0;
L->Next = NULL;
}
bool IsEmpty()
{
return L->Next == NULL;
}
void Push(int Exp, double Coe)
{
List Tmp = new LinkedList;
Tmp->Coe = Coe;
Tmp->Exp = Exp;
Tmp->Next = L->Next;
L->Next = Tmp;
NodeNumbers++;
}
void Pop()
{
if (IsEmpty())
{
cout << "Empty stack!" << endl;
return;
}
List Tmp = L->Next;
L->Next = Tmp->Next;
delete Tmp;
NodeNumbers--;
}
~Stack()
{
List P = L->Next;
while (P != NULL)
{
L->Next = P->Next;
delete P;
P = L->Next;
}
delete L;
}
void Show()
{
if (NodeNumbers == 0)
{
cout << "Empty stack!" << endl;
return;
}
List Tmp = L->Next;
cout << NodeNumbers << " ";
for (int i = 0; i < NodeNumbers - 1; i++)
{
cout << fixed << setprecision(1) << Tmp->Exp << " " << fixed<<setprecision(1)<<Tmp->Coe << " ";
Tmp = Tmp->Next;
}
cout << fixed << setprecision(1) << Tmp->Exp << " " << fixed << setprecision(1) << Tmp->Coe<<endl;
}
friend void Product(Stack& S1,Stack& S2,Stack& Res);
void MergePolynomial()//合并同类项
{
List P = L->Next;
/*if (P->Next == NULL)
return;*/
while (P->Next!= NULL)
{
if (P->Exp == P->Next->Exp)
{
List Tmp = P->Next;
P->Coe = P->Coe + Tmp->Coe;
P->Next = Tmp->Next;
delete Tmp;
NodeNumbers--;
}
P = P->Next;
if (P == NULL)
break;
}
}
void ExpDescend()
{
if (IsEmpty())
{
cout << "Empty stack!" << endl;
return;
}
int* ExpArray = new int[NodeNumbers];
List LCopy = L->Next;
for (int i = 0; i < NodeNumbers; i++)
{
ExpArray[i] = LCopy->Exp;
LCopy = LCopy->Next;
}
qsort(ExpArray, NodeNumbers, sizeof(int), cmp);
List DescendList = new LinkedList;
DescendList->Coe = DescendList->Exp = 0;
DescendList->Next = NULL;
for (int i = 0; i < NodeNumbers; i++)
{
List P = L->Next;
while (P != NULL&&P->Exp != ExpArray[i])
P = P->Next;
if (P != NULL)
{
List TmpCell = new LinkedList;
TmpCell->Coe = P->Coe;
TmpCell->Exp = P->Exp;
TmpCell->Next = NULL;
TmpCell->Next = DescendList->Next;
DescendList->Next = TmpCell;
P->Exp = -1;
}
}
L = DescendList;
}
void DeleteNonTerm()
{
if (IsEmpty())
{
cout << "Empty stack!" << endl;
return;
}
List P = L;
while (P->Next != NULL)
{
if (abs(P->Next->Coe) < pow(10, -6))
{
List Temp = P->Next;
P->Next = Temp->Next;
delete Temp;
NodeNumbers--;
}
P = P->Next;
}
}
};
void Product(Stack& S1, Stack& S2,Stack& Res)
{
List Poly1 = S1.L;
List Poly2 = S2.L;
List ProdPoly = Res.L;
if (Poly1 == NULL)
{
cout << "Sorry, Poly1 Is NULL" << endl;
return;
}
if (Poly2 == NULL)
{
cout << "Sorry, Poly2 Is NULL" << endl;
return;
}
List P1 = Poly1->Next;//P1遍历Poly1
List P2 = Poly2->Next;//P2遍历Poly2
List P3 = ProdPoly;//P3在ProdPoly上移动
while (P1!=NULL)
{
while (P2 != NULL)
{
List P = (List)malloc(sizeof(LinkedList));
P->Coe = P1->Coe*P2->Coe;
P->Exp = P1->Exp + P2->Exp;
P->Next = NULL;
P3->Next = P;
P3 = P3->Next;
P2 = P2->Next;
Res.NodeNumbers++;
}
P1 = P1->Next;
P2 = Poly2->Next;
}
Res.ExpDescend();
Res.MergePolynomial();
Res.DeleteNonTerm();
}
int main()
{
int K1, K2;
cin >> K1;
int* ExpArray1 = new int[K1];
double* CoeArray1 = new double[K1];
for (int i = 0; i < K1; i++)
cin >> ExpArray1[i] >> CoeArray1[i];
cin >> K2;
int* ExpArray2 = new int[K2];
double* CoeArray2 = new double[K2];
for (int i = 0; i < K2; i++)
cin >> ExpArray2[i] >> CoeArray2[i];
Stack S1, S2;
for (int i = 0; i < K1; i++)
{
S1.Push(ExpArray1[i], CoeArray1[i]);
}
for (int i = 0; i < K2; i++)
{
S2.Push(ExpArray2[i], CoeArray2[i]);
}
Stack Res;
Product(S1, S2, Res);
Res.Show();
delete[]ExpArray1;
delete[]CoeArray1;
delete[]ExpArray2;
delete[]CoeArray2;
return 0;
}