A1002 A+B for Polynomials (25 分)PAT甲级(C++)【多项式处理】

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 1≤K≤10,0≤NK​<⋯<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

题意分析: 

题目的意思很简单,有A和B两个多项式,我们要将它们相加,即合并同类项

对于给的测试样例:A=2.4x+3.2        B=1.5x^2+0.5x        所以C=A+B=1.5x^2+2.9x+3.2

我们可以将多项式中的每一个项表示为一个结构体Term,包含指数exp和系数coe两个属性

题目中的项是按照指数递减的顺序排列的,且不会重复,所以将两个多项式的项同时开始遍历,取指数大的那个项先放入C中,如果指数相同,则系数相加再放入C中

注意!!!测试点345是针对系数相加为0的情况,0的话就不用放入C了;测试点6是针对A+B=0的情况,单独处理,直接输出0!!!

害,都是血泪教训。。。

代码如下:

#include<iostream>
#include<vector>
using namespace std;
struct Term
{
    int exp;
    double coe;
};
int main()
{
    vector<Term> C;
    int kA;
    scanf("%d",&kA);
    vector<Term> A;
    for(int i=0;i<kA;i++)
    {
        Term a;
        int exp;
        double coe;
        scanf("%d",&exp);
        scanf("%lf",&coe);
        a.exp=exp;
        a.coe=coe;
        A.push_back(a);
    }
    int kB;
    scanf("%d",&kB);
    vector<Term> B;
    for(int i=0;i<kB;i++)
    {
        Term b;
        int exp;
        double coe;
        scanf("%d",&exp);
        scanf("%lf",&coe);
        b.exp=exp;
        b.coe=coe;
        B.push_back(b);
    }
    int i=0,j=0;
    while(i<kA||j<kB){
        if(A[i].exp>B[j].exp)
        {
            C.push_back(A[i]);
            i++;
        }
        else if(A[i].exp<B[j].exp)
        {
            C.push_back(B[j]);
            j++;
        }
        else if(A[i].exp==B[j].exp)
        {
            Term temp;
            temp.exp=A[i].exp;
            temp.coe=A[i].coe+B[j].coe;
            if(temp.coe!=0){//系数和为0则删去
                C.push_back(temp);
            }
            i++;
            j++;
        }
    }
    while(i<kA)
    {
        C.push_back(A[i]);
        i++;
    }
    while(j<kB)
    {
        C.push_back(B[j]);
        j++;
    }
    int size=C.size();
    //A+B=0的情况单独处理,不然会有段错误
    if(size==0)printf("%d",0);
    else{
        printf("%d ",size);
        printf("%d %.1lf",C[0].exp,C[0].coe);
        for(int i=1;i<C.size();i++)
        {
            printf(" %d %.1lf",C[i].exp,C[i].coe);
        }
    }
    printf("\n");
    return 0;
}

运行结果如下:

(我不理解的是,在判断语句 while( i < kA || j < kB ) 中,我一开始写的是while( i != kA || j !=kB ),这样就会报段错误,我也想不通为什么,找了很久错误,甚至以为是vector的某种原因,最后改成现在这样就ok了,希望有大佬可以评论区解释一下~~)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值