P1002 A+B for Polynomials (25分)

1002 A+B for Polynomials (25分)
 

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:

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

where K is the number of nonzero terms in the polynomial, Ni​​ and aNi​​​​ (,) are the exponents and coefficients, respectively. It is given that 1,0.

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

  0是零多项式,在题中将之归于“0项”多项式。当然了,实际是没有0项多项式的,只有零多项式,但是非要输出个结果,0还是合理的

我用了另一种思路,用数组模仿链表的实现。将A、B两多项式由次数从高到低依次计算,存入新数组。开个1000 的数组实在是浪费空间

  疑问:

    最初最后一个测试点没过的时候,我以为又是小负数近似格式的问题,便将系数绝对值小于0.05的项全忽略了。

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct Poly
{
    double coef;
    int exp;
} Poly[20];

int main(void)
{
    int Ka, Kb, Ksum = 0;
    Poly A, B, Sum;

    scanf("%d", &Ka);
    for (int i = 0; i < Ka; ++i)
    {
        scanf("%d %lf", &A[i].exp, &A[i].coef);
    }

    scanf("%d", &Kb);
    for (int i = 0; i < Kb; ++i)
    {
        scanf("%d %lf", &B[i].exp, &B[i].coef);
    }

    int i = 0, j = 0;
    while (i < Ka || j < Kb)
    { //类似于链表的多项式相加
        if (i == Ka || (j < Kb && A[i].exp < B[j].exp))
        { //多项式B长 或者 多项式B指数在A中不存在
            Sum[Ksum].exp = B[j].exp;
            Sum[Ksum].coef = B[j++].coef;
        }
        else if (j == Kb || (i < Ka && A[i].exp > B[j].exp))
        { //多项式A长 或者 多项式A指数在B中不存在
            Sum[Ksum].exp = A[i].exp;
            Sum[Ksum].coef = A[i++].coef;
        }
        else
        { //
            Sum[Ksum].exp = A[i].exp;
            Sum[Ksum].coef = A[i++].coef + B[j++].coef;
        }
        if (fabs(Sum[Ksum].coef) >= 0.05)
        { //小于这个值的被舍去了
            Ksum++;
        }
    }

    printf("%d", Ksum);

    for (int i = 0; i < Ksum; i++)
    {
        printf(" %d %.1lf", Sum[i].exp, Sum[i].coef);
    }

    return 0;
}

 

C++版本:

  用了Map和vector,MAP, 将整型的exp最为Map的关键字,再用二维的vector存储结果

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main(void)
{
    map<int, float> poly1;
    int K, exp;
    float cof;

    scanf("%d", &K);
    for (int i = 0; i < K; ++i)
    {

        scanf("%d%f", &exp, &cof);
        poly1[exp] += cof;
    }

    scanf("%d", &K);
    for (int i = 0; i < K; ++i)
    {

        scanf("%d%f", &exp, &cof);
        poly1[exp] += cof;
    }

    vector<pair<int, float>> res;
    for (map<int, float>::reverse_iterator it = poly1.rbegin(); it != poly1.rend(); it++)
    {
        if (it->second != 0) // 两个系数和为0的得过滤掉
        {
            res.push_back(make_pair(it->first, it->second));
        }
    }

    printf("%lu", res.size());
    for (int i = 0; i < res.size(); ++i)
    {
        printf(" %d %.1f", res[i].first, res[i].second);
    }
    return 0;
}

 

PAT不易,诸君共勉!

posted @ 2020-02-19 15:27  秦_殇  阅读(179)  评论(0编辑  收藏  举报