设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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 <iostream>
#define MAX 200000
using namespace std;
class mulproject;
class headmul;
headmul& insertmul(headmul&hd, int coe, int ind);
headmul&multiply(headmul&hd, const headmul &hd2, headmul&h3);
headmul&add(headmul&hd, const headmul&hd2, headmul&hd3);
void freemul(headmul&hd);
class mulproject{
public:
int coe;
int ind;
mulproject*next;
};
class headmul
{
public:
mulproject*first;
};
int main()
{
int coe[MAX];
int N1, N2, index, count = 0;
headmul hdone, hdtwo, hdthree, hdfour;
mulproject *tmp;
hdone.first = NULL;
hdtwo.first = NULL;
hdthree.first = NULL;
hdfour.first = NULL;
cin >> N1;
for (int i = 0; i < N1; i++)
{
cin >> coe[i];
cin >> index;
insertmul(hdone, coe[i], index);
}
cin >> N2;
for (int i = 0; i < N2; i++)
{
cin >> coe[i];
cin >> index;
insertmul(hdtwo, coe[i], index);
}
multiply(hdone, hdtwo, hdthree);
tmp = hdthree.first;
while (tmp)
{
if (tmp->coe != 0)
{
count++;
if (count > 1)
{
cout << " " << tmp->coe << " " << tmp->ind;
}
else
{
cout << tmp->coe << " " << tmp->ind;
}
}
tmp = tmp->next;
}
if (count == 0)
cout << "0 0";
count = 0;
cout << endl;
add(hdone, hdtwo, hdfour);
tmp = hdfour.first;
while (tmp)
{
if (tmp->coe != 0)
{
count++;
if (count>1)
{
cout << " " << tmp->coe << " ";
cout << tmp->ind;
}
else
{
cout << tmp->coe << " ";
cout << tmp->ind;
}
}
tmp = tmp->next;
}
if (count == 0)
cout << "0 0";
cout << endl;
freemul(hdone);//释放链表
freemul(hdtwo);
freemul(hdthree);
freemul(hdfour);
system("pause");
return 0;
}
headmul& insertmul(headmul&hd, int coe, int ind)
{
mulproject* tmp = (mulproject*)malloc(sizeof(mulproject));
mulproject*ele = hd.first;
mulproject*pre = ele;
tmp->coe = coe;
tmp->ind = ind;
if (hd.first)
{
if (ind > hd.first->ind)
{
tmp->next = hd.first;
hd.first = tmp;
return hd;
}
else
{
while (ele)
{
if (ele->ind<ind)
{
tmp->next = pre->next;
pre->next = tmp;
return hd;
}
else
{
if (ele->ind == ind)
{
ele->coe = ele->coe + coe;
return hd;
}
else
{
pre = ele;
ele = ele->next;
}
}
}
tmp->next = NULL;
pre->next = tmp;
return hd;
}
}
else//空时
{
tmp->next = NULL;
hd.first = tmp;
return hd;
}
}
headmul&multiply(headmul&hd, const headmul &hd2, headmul&hd3)
{//输出系数不为零
if (hd.first != NULL&&hd2.first != NULL)
{
mulproject*tmp1 = hd.first;
mulproject*tmp2 = hd2.first;
mulproject*tmp = hd3.first;
int tmpcoe = 0;
int tmpind = 0;
while (tmp1)
{
while (tmp2)
{
tmpcoe = tmp1->coe*tmp2->coe;
tmpind = tmp1->ind + tmp2->ind;
insertmul(hd3, tmpcoe, tmpind);
tmp2 = tmp2->next;
}
tmp2 = hd2.first;
tmp1 = tmp1->next;
}
}
else
{
return hd;
}
}
headmul&add(headmul&hd, const headmul&hd2, headmul&hd3)
{
mulproject*tmp = hd2.first;
while (tmp)
{
insertmul(hd3, tmp->coe, tmp->ind);
tmp = tmp->next;
}
tmp = hd.first;
while (tmp)
{
insertmul(hd3, tmp->coe, tmp->ind);
tmp = tmp->next;
}
return hd3;
}
void freemul(headmul&hd)
{
mulproject*tmp = hd.first;
while (tmp)
{
mulproject*nexm = tmp->next;
free(tmp);
tmp = nexm;
}
}
转载请注明原文地址: https://ju.6miu.com/read-665077.html