【个人代码及思路】PAT甲级:1002 A+B for Polynomials (25分)

PAT (Advanced Level) Practice

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:

K N1 a N 1 N2 a N2 … 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<⋯<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 

个人代码:

#include<stdio.h>

int main()
{
    //C为最后的输出
    int Ak, Bk, Ck; //K值
    
    //输入参数
    scanf("%d", &Ak);
    int An[Ak-1];
    double Aa[Ak-1];
    for(int i = 0; i < Ak; i++)
    {
        scanf("%d %lf", &An[i], &Aa[i]);
    }

    scanf("%d", &Bk);
    int Bn[Ak-1];
    double Ba[Bk-1];
    for(int i = 0; i < Bk; i++)
    {
        scanf("%d %lf", &Bn[i], &Ba[i]);
    }

    //确定Ck
    Ck = Ak;
    for(int i = 0; i < Bk; i++)
    {
        int Ckplus = 1;
        for(int j = 0; j < Ak; j++)
        {
            if(Bn[i] == An[j])
            {
                Ckplus = 0;
                break;
            } 
        }
        Ck += Ckplus;
    }

    //构造Dn合并An和Bn,便于确定Cn
    int Dk = Ak + Bk;
    int Dn[Dk-1];
    for(int i = 0; i< Dk; i++)
    {
        if(i < Ak)
        {
            Dn[i] = An[i];
        }
        else
        {
            Dn[i] = Bn[i-Ak];
        }
    }

    //确定Cn
    int Cn[Ck-1];
    Cn[0] = 1001;
    for(int i = 0; i < Ck; i++)
    {
        //每轮循环确定一次Dn中未确定的最大值
        int max = -1;
        
        for(int j = 0; j < Dk; j++)
        {
            if(i == 0)
            {
                if(Dn[j] > max)
                {
                    max = Dn[j];
                }                       
            }
            else
            {
                if((Dn[j] > max) && (Dn[j] < Cn[i-1]))
                {
                    max = Dn[j];
                }
            }
        }

        Cn[i] = max;
    }

    //确定Ca
    double Ca[Ck-1];
    for(int i = 0; i < Ck; i++)
    {
        Ca[i] = 0;
        for(int j = 0; j < Ak; j++)
        {
            if(Cn[i] == An[j])
            {
                Ca[i] += Aa[j];
            }
        }

        for(int j = 0; j < Bk; j++)
        {
            if(Cn[i] == Bn[j])
            {
                Ca[i] += Ba[j];
            }
        }
    }

    //输出
    int Cksub = 0;
    for(int i = 0 ; i < Ck; i++)
    {
        if(Ca[i] == 0)
        {
            Cksub++;
        }  
    }
    Cksub = Ck - Cksub;
    printf("%d", Cksub);
    for(int i = 0 ; i < Ck; i++)
    {
        if(Ca[i] == 0)
        {
            continue;
        }
        printf(" %d %.1f", Cn[i], Ca[i]);
    }
    return 0;
}

思路提示:

代码虽然很长,但是内容很简单,可以简化代码,这里就不简化了。说下解题的思路:

  • 存放好输入参数后,先看需要输出的第一个数字Ck,通过An和Bn中不同数值个数的和来确定;

  • 有了Ck之后便是输出一个指数,输出一个系数。指数确定好,系数就通过对应的Aa和Ba数组相加就可以了。指数Cn可以通过构造数组Dn(Dn是An和Bn的合并),每次循环找出Dn中的最大值即可确定Cn;

  • Cn中对应指数的系数Aa+Ba即得到Ca;

  • 按照格式输出即可

提交答案后发现只通过了三个,仔细一想这样输出会把系数是0的指数也输出了,而实际上不应该输出系数为0的项,同理对应Ck也要减小,于是有了最终代码。另外,代码很多地方可以简化,强迫症原因,暂时不修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值