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 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 1≤K≤10,0≤N?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;
}

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

时间: 2024-10-13 12:28:37

1002 A+B for Polynomials (PAT (Advanced Level) Practice)的相关文章

1002. A+B for Polynomials (25)——PAT (Advanced Level) Practise

题目信息: 1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file contains one test case. Each case occupies 2 lin

1009. Product of Polynomials (25)——PAT (Advanced Level) Practise

题目信息: 1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 c

PAT (Advanced Level) Practice 1011 World Cup Betting (20 分)

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their

PAT (Advanced Level) Practice 1068 Find More Coins

题解 01背包板子 + 记录路径.这次的记录路径比较特殊,要从多组解中找到一组由尽量小价值的硬币组成的解.所以不能利用一维数组记录路径,path[目前重量] = 物品序号,因为这样最后只能记录一个可能符合或不符合要求解.所以应该利用二维数组记录路径,path[ 物品序号 ][ 目前重量 ] = 1,这样可以记录多组解.因为要求为找到最小的一组解,所以先将拥有的硬币从大到小排序,以便于进行01背包时,可以从大到小更新解. 代码 #include<bits/stdc++.h> using name

Pat(Advanced Level)Practice--1060(Are They Equal)

Pat1060代码 题目描述: If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with simple chopping. Now given the number of significant digits on a machine and two flo

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names