本题作为第一次上机的题目,确实有一定的难度,针对后面不同的情况需要分情况进行设计算法。并且要读懂题目的要求,不然肯定是做不对的(我就是没读懂题目,嘎嘎写完发现超时报错)。本代码应该是优快云中唯一一个可以运行并且满分的代码,也是我根据其他代码进行参考学习后才写出来的(读书人的的事嘛,dddd),在此对提供思路和代码的大佬表示感谢。也欢迎各位和我进行深入交流。
问题描述
给定两个多项式,求解其和与差。多项式的项数为M,而最高幂次为N。(1<=M<=10,1<=N<=1000000)
输入说明
输入包含了两个多项式,分为两行给出(同行数据间以空格隔开):
每一行为两组数据:第一组为一个值,表示多项式的非零项数M;第二组为2*M个值,每相邻两个值表达一个多项式的非零系数项,分别为系数值、幂次值(其中各项按照幂次的降序排列)。但如果多项式没有非零系数项,则仅用0(M的值)表示,后面没有系数和幂次值出现。例如,第一行的数据为:4 1 10 2 5 3 4 4 0,那么表示多项式有4项,对应的多项式为:x^10+2x^5+3x^4+4. 又例如,第二行的数据为:4 1 8 -2 5 3 3 4 1,表示多项式有4项,对应的多项式为:x^8-2x^5+3x^3+4x。那么上述两个多项式相加的输出结果应为:6 1 10 1 8 3 4 3 3 4 1 4 0,对应的多项式为:x^10+x^8+3x^4+3x^3+4x+4.第一个多项式减去第二个多项式的输出结果为:7 1 10 -1 8 4 5 3 4 -3 3 -4 1 4 0,对应多项式:x^10-x^8+4x^5+3x^4-3x^3-4x+4.
输出说明
输出包含了两个多项式,分为两行给出(同行数据间以空格隔开):
第一行是多项式相加的结果,第二行是多项式相减的结果。每一行为两组数据:第一组为一个值,表示多项式的非零项数M;第二组为2*M个值,每相邻两个值表达一个多项式的非零系数项,分别为系数值、幂次值(其中各项按照幂次的降序排列)。但如果多项式没有非零系数项,则仅用0(M的值)表示,后面没有系数和幂次值出现。
输入样例
例1:
4 1 10 2 5 3 4 4 0
4 1 8 -2 5 3 3 4 1
输出样例
例1:
6 1 10 1 8 3 4 3 3 4 1 4 0
7 1 10 -1 8 4 5 3 4 -3 3 -4 1 4 0
提示:
用链表表示多项式
#include<malloc.h>
#include<iostream>
#include<string>
using namespace std;
typedef struct PolyTerm* SqPoly; //多项式指针指向下一位
struct PolyTerm //定义多项式
{
int length; //项数
int coef; //系数
int exp; //指数
SqPoly next;
};
SqPoly init(); //初始化分配空间(记得写返回值)
SqPoly SqInit(SqPoly sq); //初始化多项式并返回(记得写返回值) 设置了头结点
void PrintSq(SqPoly sq); //打印多项式
SqPoly AddSq(SqPoly s1, SqPoly s2); //多项式的求和,返回新的多项式
SqPoly SubSq(SqPoly s1, SqPoly s2); //多项式的求差,返回新的多项式
int main()
{
SqPoly addsq, subsq, sq1, sq2;
sq1 = init();
sq2 = init();
sq1 = SqInit(sq1);
sq2 = SqInit(sq2);
addsq = AddSq(sq1, sq2);
PrintSq(addsq);
subsq = SubSq(sq1, sq2);
PrintSq(subsq);
return 0;
}
SqPoly init()
{
SqPoly sq = (SqPoly)malloc(sizeof(struct PolyTerm));
sq->next = NULL;
return sq;
}
SqPoly SqInit(SqPoly sq)
{
SqPoly start = sq;
SqPoly s = init();
int len;
cin >> len;
s->length = len;
for (int i = 0; i < len; i++)
{
cin >> s->coef;
cin >> s->exp;
sq->next = s;
sq = s;
s = init();
}
return start;
}
void PrintSq(SqPoly sq)
{
if (sq == NULL)
{
cout << 0 << endl;
return;
}
sq = sq->next;
cout << sq->length << " ";
while (sq)
{
cout << sq->coef << " " << sq->exp << " ";
if (sq->next == NULL)
{
cout << endl;
return;
}
sq = sq->next;
}
cout << endl;
}
SqPoly AddSq(SqPoly s1, SqPoly s2)
{
int len = 0;
SqPoly start1 = s1;
SqPoly start2 = s2;
s1 = s1->next;
s2 = s2->next;
SqPoly sq = (SqPoly)malloc(sizeof(struct PolyTerm));
SqPoly start = sq; //给最开始的位置记住
SqPoly temp = init();
while (s1 && s2)
{
if (s1->exp > s2->exp)
{
temp->exp = s1->exp;
temp->coef = s1->coef;
sq->next = temp;
sq = temp;
s1 = s1->next;
len++;
}
else if (s1->exp < s2->exp)
{
temp->exp = s2->exp;
temp->coef = s2->coef;
sq->next = temp;
sq = temp;
s2 = s2->next;
len++;
}
else
{
temp->coef = s1->coef + s2->coef;
temp->exp = s1->exp;
if (temp->coef == 0)
{
s1 = s1->next;
s2 = s2->next;
temp = init();
continue;
}
sq->next = temp;
sq = temp;
s1 = s1->next;
s2 = s2->next;
len++;
}
temp = init();
}
while (s1)
{
temp->exp = s1->exp;
temp->coef = s1->coef;
sq->next = temp;
sq = temp;
s1 = s1->next;
temp = init();
len++;
}
while (s2)
{
temp->exp = s2->exp;
temp->coef = s2->coef;
sq->next = temp;
sq = temp;
s2 = s2->next;
temp = init();
len++;
}
s1 = start1;
s2 = start2;
if (len == 0)
return NULL;
start->next->length = len;
return start;
}
SqPoly SubSq(SqPoly s1, SqPoly s2)
{
int len = 0;
SqPoly start1 = s1;
SqPoly start2 = s2;
s1 = s1->next;
s2 = s2->next;
SqPoly sq = (SqPoly)malloc(sizeof(struct PolyTerm)); //新的多项式,用于接收结果
SqPoly start = sq;
SqPoly temp = init();
while (s1 && s2)
{
if (s1->exp > s2->exp)
{
temp->exp = s1->exp;
temp->coef = s1->coef;
sq->next = temp;
sq = temp;
s1 = s1->next;
len++;
}
else if (s1->exp < s2->exp)
{
temp->exp = s2->exp;
temp->coef = -s2->coef;
sq->next = temp;
sq = temp;
s2 = s2->next;
len++;
}
else
{
temp->coef = s1->coef - s2->coef;
temp->exp = s1->exp;
if (temp->coef == 0)
{
s1 = s1->next;
s2 = s2->next;
temp = init();
continue;
}
sq->next = temp;
sq = temp;
s1 = s1->next;
s2 = s2->next;
len++;
}
temp = init();
}
while (s1)
{
temp->exp = s1->exp;
temp->coef = s1->coef;
sq->next = temp;
sq = temp;
s1 = s1->next;
temp = init();
len++;
}
while (s2)
{
temp->exp = s2->exp;
temp->coef = -s2->coef;
sq->next = temp;
sq = temp;
s2 = s2->next;
temp = init();
len++;
}
s1 = start1;
s2 = start2;
if (len == 0)
return NULL;
start->next->length = len;
return start;
}