C-OJ提交出现段错误?
问题是在https://pta.patest.cn/pta/test/558/exam/4/question/8660
我自己写的代码自己运行正常,没有报错,但是提交的时候总是显示 段错误 ,不知道怎么回事,求大神指教。
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int Coefficient;
int Exponent;
struct _node *next;
} Node;
typedef struct _list {
Node * head;
} List;
void insert(List * pList, int coeff, int expon);
void print(const List *pList);
void attach(int coeff, int expon, List * pList);
void add(List * list1, List * list2, List * listSum);
void multy(List * list1, List * list2, List * listProd);
int main()
{
List list1, list2, listSum, listProd;
list1.head = NULL;
list2.head = NULL;
listSum.head = NULL;
listProd.head = NULL;
int n, m;
int i;
int coeff, expon;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d %d", &coeff, &expon);
insert(&list1, coeff, expon);
}
scanf("%d", &m);
for (i = 0; i < m; i++) {
scanf("%d %d", &coeff, &expon);
insert(&list2, coeff, expon);
}
multy(&list1, &list2, &listProd);
print(&listProd);
add(&list1, &list2, &listSum);
print(&listSum);
return 0;
}
void insert(List * pList, int coeff, int expon)
{
Node *p = (Node *)malloc(sizeof(Node));
p->Coefficient = coeff;
p->Exponent = expon;
p->next = NULL;
Node *last = pList->head;
if (last) {
while (last->next) {
last = last->next;
}
last->next = p;
}
else {
pList->head = p;
}
}
void print(const List *pList)
{
Node *p;
p = pList->head;
if (!p) {
printf("0 0\n");
return ;
}
for (p = pList->head; p; p = p->next) {
//printf("hr1\n");
printf("%d %d", p->Coefficient, p->Exponent);
if (p->next) {
printf(" ");
}
}
printf("\n");
}
void add(List * list1, List * list2, List * listSum)
{
Node *last1 = list1->head;
Node *last2 = list2->head;
//List lastSum;
//lastSum.head = listSum->head;
while (last1 && last2) {
//printf("hr1\n");
if (last1->Exponent > last2->Exponent) {
attach(last1->Coefficient, last1->Exponent, listSum);
last1 = last1->next;
}
else if (last1->Exponent < last2->Exponent) {
attach(last2->Coefficient, last2->Exponent, listSum);
last2 = last2->next;
}
else {
if (last1->Coefficient + last2->Coefficient )
attach(last1->Coefficient + last2->Coefficient, last1->Exponent, listSum);
last1 = last1->next;
last2 = last2->next;
}
}
for (; last1; last1 = last1->next) {
//if last
attach(last1->Coefficient, last1->Exponent, listSum);
}
for (; last2; last2 = last2->next) {
attach(last1->Coefficient, last2->Exponent, listSum);
}
}
void attach(int coeff, int expon, List * pList)
{
Node *p = (Node *)malloc(sizeof(Node));
p->Coefficient = coeff;
p->Exponent = expon;
p->next = NULL;
Node *last = pList->head;
if (last) {
while (last->next) {
last = last->next;
}
last->next = p;
}
else {
pList->head = p;
}
}
void multy(List * list1, List * list2, List * listProd)
{
if (!(list1->head) || !(list2->head)) {
return ;
}
Node *last1 = list1->head;
Node *last2 = list2->head;
Node *lastProd = listProd->head;
Node *t;
int coeff, expon;
for (; last1; last1 = last1->next) {
attach(last1->Coefficient * last2->Coefficient, last1->Exponent + last2->Exponent, listProd);
}
//print(listProd);
last2 = last2->next;
//printf("hr3\n");
for (; last2; last2 = last2->next) {
lastProd = listProd->head;
for (last1 = list1->head; last1; last1 = last1->next) {
coeff = last1->Coefficient * last2->Coefficient;
expon = last1->Exponent + last2->Exponent;
//printf("hr4\n");
while (lastProd->next && lastProd->next->Exponent > expon)
lastProd = lastProd->next;
if (lastProd->next && lastProd->next->Exponent == expon) {
if (lastProd->next->Coefficient + coeff)
lastProd->next->Coefficient += coeff;
else {
t = lastProd->next;
lastProd->next = t->next;
free(t);
}
}
else {
t = (Node *)malloc(sizeof(Node));
t->Coefficient = coeff;
t->Exponent = expon;
t->next = lastProd->next;
lastProd->next = t;
lastProd = lastProd->next;
}
}
}
}
我自己在dev gcc 4.9.2 运行均没有出现任何错误和warning.但是不知道在页面提交时就出现段错误了,不知道怎么回事,求大神指教:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下输入会出现段错误。
问题出在下面: