设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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
结尾无空行
下面是测试点提示:
0 | sample换个数字 | 答案正确 | 12 | 5 ms | 688 KB |
1 | 同类项合并时有抵消 | 答案正确 | 4 | 5 ms | 556 KB |
2 | 系数和指数取上限,结果有零多项式 | 答案正确 | 2 | 4 ms | 572 KB |
3 | 输入有零多项式和常数多项式 | 答案正确 | 2 | 4 ms | 572 KB |
再看一遍输出要求:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0
也就是说输出的时候乘法或者加法都是一个多项式,当这个多项式是零项多项式时候输出0 0;而不是说没输入的时候输出0 0;输入时应该输入0(第一位数字是个数的意思),此时建立的链表返回 空。
列出两种方法:(难点在于处理数据格式,0的产生很烦)
#include<iostream>
using namespace std;
int main()
{
int a[1001] = { 0 }, b[1001] = { 0 }, c[1001] = { 0 },d[2001] = {0};
int num_a, num_b;
int coef, expo;
cin >> num_a;
int max_a=0, max_b=0;
for (int i = 0; i < num_a; i++)
{
cin >> coef >> expo;
if (max_a == 0) max_a = expo;
a[expo] = coef;
}
cin >> num_b;
for (int i = 0; i < num_b; i++)
{
cin >> coef >> expo;
if (max_b == 0)max_b = expo;
b[expo] = coef;
}
int max = max_a > max_b ? max_a : max_b;
//相乘模块
int cnt = 0;
int max2 = max_a+max_b;
for (int i = 0; i <= max_a; i++)
{
for (int j = 0; j <= max_b; j++)
{
d[i + j]+= a[i] * b[j];
}
}
for (int i = max2; i >= 0; i--)
{
if (d[i] != 0)
{
cnt++;
cout << d[max2] << " " << max2;
break;
}
}
for (int i = max2 - 1; i >= 0; i--)
{
if (d[i] != 0)
{
cnt++;
cout << " " << d[i] << " " << i;
}
}
if (cnt == 0)cout << "0 0";//0 2 3 2 1 0测试点4
cout << endl;
//相加模块
cnt = 0;
for (int i = 0; i <=max; i++)
{
c[i] = a[i] + b[i];
}
for (int i = max; i >=0; i--)
{
if (c[i] != 0)
{
cnt++;
cout <<c[i] << " " << i;
max = i;
break; //此处处理的目的是不知道从c[0]要不要打印,会破坏格式,以及max是否为0也未知。
}
}
for (int i = max - 1; i >= 0; i--)
{
if (c[i] != 0)
{
cnt++;
cout << " " << c[i] << " " << i;
}
}
if ( cnt==0)
{
cout<< "0 0";
}
system("pause");
return 0;
}
#include<iostream>
using namespace std;
class Node
{
public:
int coef;
int expo;
Node* next;
};
typedef Node* ptr;
ptr MakeList() //建立链表
{
int num,c,e;
cin >> num;
ptr head = NULL, tail = NULL,p=NULL;
while (num--)
{
cin >> c >> e;
p = (ptr)malloc(sizeof(Node));
p->coef = c;
p->expo = e;
p->next = NULL;
if (head == NULL)
head = p;
else
tail->next = p; //记得与上一个结点链接
tail = p;
}
return head;
}
void AddList(ptr L1,ptr L2)
{
ptr head = NULL,tail=NULL,p=NULL;
while (L1 && L2)
{
p = (ptr)malloc(sizeof(Node));
if (L1->expo > L2->expo)
{
p->expo = L1->expo;
p->coef = L1->coef;
L1 = L1->next; //每一次的指针后移不要忘了
}
else if (L1->expo <L2->expo)
{
p->expo = L2->expo;
p->coef = L2->coef;
L2 = L2->next;
}
else
{
if ((L1->coef + L2->coef) == 0)
{
free(p);
p = NULL;
}
else
{
p->expo = L1->expo;
p->coef = L1->coef + L2->coef;
}
L1 = L1->next;
L2 = L2->next;
}
if (p)
{
p->next = NULL;
if (head == NULL) //链接的操作其实可以写个attach函数,减少重复代码
head = p;
else
tail->next = p;
tail = p;
}
}
for (; L1 != NULL; L1 = L1->next)
{
p = (ptr)malloc(sizeof(Node));
p->expo = L1->expo;
p->coef = L1->coef;
p->next = NULL;
if (head == NULL)
head = p;
else
tail->next = p; //链接的函数可简化此处
tail = p;
}
for (; L2 != NULL; L2 = L2->next)
{
p = (ptr)malloc(sizeof(Node));
p->expo = L2->expo;
p->coef = L2->coef;
p->next = NULL;
if (head == NULL)
head = p;
else
tail->next = p;
tail = p;
}
if (head == NULL)
{
cout << "0 0" << endl;
}
else
{
cout << head->coef << " " << head->expo;
head = head->next;
while (head)
{
cout <<" "<< head->coef << " " << head->expo;
head = head->next;
}
}
}
void MulList(ptr L1,ptr L2)
{
ptr head = NULL, p = NULL;
if (!L1 || !L2)
{
cout << "0 0" << endl;
return;
}
p = (ptr)malloc(sizeof(Node));
p->next = NULL; //先建立一个链表头节点很重要,否则后面的rear->next不一定存在,当rear为空
ptr temp = L2;
while (L1)
{
ptr rear = p;
L2 = temp; // L2要归位到头节点
while (L2)
{
int c = L1->coef * L2->coef;
int e = L1->expo + L2->expo;
if (c != 0)
{
while (rear->next && rear->next->expo>e)
rear = rear->next;
if (rear->next && rear->next->expo == e)
{
if (rear->next->coef +c== 0)
{
rear->next = rear->next->next;
}
else
{
rear->next->coef += c;
}
}
else
{
ptr t = (ptr)malloc(sizeof(Node));
t->coef = c;
t->expo = e;
t->next = rear->next;
rear->next = t;
rear = t;
}
}
L2 = L2->next;
}
L1 = L1->next;
}
p = p->next;
cout << p->coef << " " << p->expo ;
p = p->next;
while (p)
{
cout <<" " << p->coef << " " << p->expo;
p = p->next;
}
cout << endl;
}
int main()
{
ptr L1=MakeList();
ptr L2=MakeList();
MulList(L1,L2);
AddList(L1, L2);
system("pause");
return 0;
}