码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-04-18 22:13:04      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:col   otto   def   inner   end   eset   one   ble   while   

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 N?1?? a?N?1???? N?2?? a?N?2???? ... N?K?? a?N?K????

where K is the number of nonzero terms in the polynomial, N?i?? and a?N?i???? (i=1,2,?,K) are the exponents and coefficients, respectively. It is given that 1K10,0N?K??<?<N?2??<N?1??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;
}

 

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

标签:col   otto   def   inner   end   eset   one   ble   while   

原文地址:https://www.cnblogs.com/Ido-911/p/10732348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!