C-OJ提交出现段错误?

发布于 2022-09-02 10:22:39 字数 5489 浏览 34 评论 0

问题是在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九厘米的零° 2022-09-09 10:22:40

以下输入会出现段错误。

0
1 1 0

问题出在下面:

void add(List * list1, List * list2, List * listSum)
{
    /* ...... */
    for (; last2; last2 = last2->next) {
        attach(last1->Coefficient, last2->Exponent, listSum);
        //     ^^^^^ last1 可能为 NULL
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文