1002 A+B for Polynomials (PAT (Advanced Level) Practice)

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N1​​ aN1​​​​ N2​​ aN2​​​​ ... NK​​ aNK​​​​

where K is the number of nonzero terms in the polynomial, Ni​​ and aNi​​​​ (i=1,2,,K) are the exponents and coefficients, respectively. It is given that 1K10,0NK​​<<N2​​<N1​​1000.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

题目很简单,说一下可能的wa点:
1. 末尾不要有空格。
2. 两项相加可能为零,此时要删除此项。
代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node *ptonext;
typedef ptonext ploy, list;
struct node{
    float n;
    int e;
    ptonext next;
    ptonext pro;
};
list A, B;
list res;
ptonext A_head, A_tail;
ptonext B_head, B_tail;
ptonext r_head, r_tail;
int cnt;
list init()
{
    ptonext T;
    T = (list)malloc(sizeof(list));
    T->n = 0;
    T->e = 0;
    T->pro = NULL;
    T->next = NULL;
    return T;
}
void caculate()
{
    ptonext a, b, r;
    ptonext T;
    a = A_head;
    b = B_head;
    r = r_head;
    a = a->next;
    b = b->next;
    while(a != A_tail && b != B_tail)
    {
        if(a->e == b->e){
            T = (ptonext)malloc(sizeof(ptonext));
            T->e = a->e;
            T->n = a->n + b->n;
            if(T->n == 0){
                a = a->next;
                b = b->next;
                continue;
            }
            T->pro = r;
            r->next = T;
            r = r->next;
            a = a->next;
            b = b->next;
            cnt++;
        }
        else if(a->e > b->e){
            T = (ptonext)malloc(sizeof(ptonext));
            T->e = a->e;
            T->n = a->n;
            r->next = T;
            T->pro = r;
            r = r->next;
            a = a->next;
            cnt++;
        }
        else{
            T = (ptonext)malloc(sizeof(ptonext));
            T->e = b->e;
            T->n = b->n;
            r->next = T;
            T->pro = r;
            r = r->next;
            b = b->next;
            cnt++;
        }
    }
    while(a != A_tail){
        T = (ptonext)malloc(sizeof(ptonext));
        T->e = a->e;
        T->n = a->n;
        r->next = T;
        T->pro = r;
        r = r->next;
        a = a->next;
        cnt++;
    }
    while(b != B_tail){
        T = (ptonext)malloc(sizeof(ptonext));
        T->e = b->e;
        T->n = b->n;
        r->next = T;
        T->pro = r;
        r = r->next;
        b = b->next;
        cnt++;
    }
    r->next = r_tail;
    r_tail->pro = r;
}
int main()
{
    int k;
    float n;
    int e;
    A = init();
    B = init();
    res = init();
    A_tail = init();
    B_tail = init();
    r_tail = init();
    A_head = A;
    B_head = B;
    r_head = res;
    cnt = 0;
    scanf("%d", &k);
    while(k--){
        scanf("%d %f", &e, &n);
        ptonext T;
        T = (ptonext)malloc(sizeof(ptonext));
        T->n = n;
        T->e = e;
        A->next = T;
        T->pro = A;
        A = A->next;
    }
    A->next = A_tail;
    A_tail->pro = A;
    scanf("%d", &k);
    while(k--){
        scanf("%d %f", &e, &n);
        ptonext T;
        T = (ptonext)malloc(sizeof(ptonext));
        T->n = n;
        T->e = e;
        B->next = T;
        T->pro = B;
        B = B->next;
    }
    B->next = B_tail;
    B_tail->pro = B;
    caculate();
    printf("%d", cnt);
    while(res->next != r_tail){
        printf(" %d %.1f", res->next->e, res->next->n);
        res = res->next;
    }
    printf("\n");
//    system("pause");
    return 0;
}

 

posted @ 2019-04-18 21:03  许卿一世长安  阅读(176)  评论(0编辑  收藏  举报