原题地址
https://pintia.cn/problem-sets/1268384564738605056/problems/1271415149946912770
解题思路
建议收看MOOC对应章节的小白专场~~
注意事项
写add函数的时候不认真,初始化一个指针变量p之后忘记写p->link = NULL导致测试点三一直不过...这里链表最后不是指向NULL的话,如果是零多项式就没法判断并输出0 0了,导致错误。
大家写的时候还是要细心一点~
参考代码
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef double db;
typedef long long LL;
typedef vector<int> VI;
typedef struct polyNode *polyNominal;
const int inf = 2e9;
const LL INF = 8e18;
const int maxn = 5e5 + 5;
struct polyNode {
int coef;
int expon;
polyNominal link;
};
//在rear节点后插入新节点p
void attach(int c, int e, polyNominal *pRear) {
polyNominal p;
p = (polyNominal)malloc(sizeof(struct polyNode));
p->coef = c;
p->expon = e;
p->link = NULL;
(*pRear)->link = p;
*pRear = p;
}
//读入多项式,返回链表头节点指针
polyNominal read() {
polyNominal p, rear, t;
int c, e, N;
scanf("%d", &N);
p = (polyNominal)malloc(sizeof(struct polyNode));
p->link = NULL;
rear = p;
while (N--) {
scanf("%d%d", &c, &e);
attach(c, e, &rear); //引用的是变量的地址,即这里是指针变量的指针,所以等下要用*
}
t = p; p = p->link; free(t); //删除空的头结点
return p;
}
//将两个多项式相加的函数,返回得到头结点指针
polyNominal add(polyNominal p1, polyNominal p2) {
polyNominal t1, t2, p, rear, t;
t1 = p1; t2 = p2;
p = (polyNominal)malloc(sizeof(struct polyNode));
p->link = NULL; //找这个错找了好久,忘记写这个导致测试点3一直过不了
rear = p;
//将t1和t2进行相加,若有一个链表处理完即退出循环
while (t1 && t2) {
if (t1->expon == t2->expon) {
if (t1->coef + t2->coef) { //同类项相加之后不为0,再进行插入
attach(t1->coef + t2->coef, t1->expon, &rear);
}
t1 = t1->link;
t2 = t2->link;
} else if (t1->expon > t2->expon) {
attach(t1->coef, t1->expon, &rear);
t1 = t1->link;
} else {
attach(t2->coef, t2->expon, &rear);
t2 = t2->link;
}
}
//处理可能非空的链表,接到p后面
while (t1) {
attach(t1->coef, t1->expon, &rear);
t1 = t1->link;
}
while (t2) {
attach(t2->coef, t2->expon, &rear);
t2 = t2->link;
}
/*
while (t2) {
attach(t2->coef, t2->expon, &rear);
}*/
//释放空的头结点
t = p; p = p->link; free(t);
return p;
}
//将多项式输出
void printPoly(polyNominal p) {
int flag = 0; //辅助调整输出格式
if (!p) {
printf("0 0\n");
return;
}
while (p) {
if (!flag) flag = 1;
else printf(" ");
printf("%d %d", p->coef, p->expon);
p = p->link;
}
printf("\n");
}
//将两个多项式相乘的函数
polyNominal mult(polyNominal p1, polyNominal p2) {
polyNominal p, rear, t1, t2, t;
int c, e;
/*判断:如果p1或p2是零项则返回NULL*/
if (!p1 || !p2) return NULL;
p = (polyNominal)malloc(sizeof(struct polyNode));
p->link = NULL;
rear = p;
t1 = p1; t2 = p2;
//先让p1的第一项和p2所有相乘,得到p
while (t2) {
attach(t1->coef * t2->coef, t1->expon + t2->expon, &rear);
t2 = t2->link;
}
/* printf("p1相乘结果为:");
printPoly(p);
printf("\n");*/
t1 = t1->link;
while (t1) {
t2 = p2;
rear = p; /*容易忘记初始化rear!!!*/
while (t2) {
e = t1->expon + t2->expon;
c = (t1->coef) * (t2->coef);
/*难点:判断在哪里进行插入/进行系数相加*/
while (rear->link && rear->link->expon > e) {
rear = rear->link;
}
if (rear->link && rear->link->expon == e) {
if (rear->link->coef + c)
rear->link->coef += c;
else {
t = rear->link;
rear->link = t->link;
free(t);
}
}
else {
t = (polyNominal)malloc(sizeof(struct polyNode));
t->coef = c;
t->expon = e;
t->link = rear->link;
rear->link = t;
rear = rear->link;
}
t2 = t2->link;
}
t1 = t1->link;
/* printf("此时为:");
printPoly(p);
printf("\n");*/
}
t = p; p = p->link; free(t);
return p;
}
int main() {
polyNominal p1, p2, pp, ps;
p1 = read(); p2 = read();
//cout << "p1:" << p1 << endl;
pp = add(p1, p2);
//cout << "add:" << pp;
ps = mult(p1, p2);
printPoly(ps);
printPoly(pp);
return 0;
}