PAT甲级1002. A+B for Polynomials

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 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

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

题目要求:

两个多项式相加,多项式的输入为 K N1 aN1 N2 aN2 … 其中K代表项数,N1…代表指数,aN1代表系数,每个多项式的项数不超过10;要求将两个多项式相加后输出非零项,输出格式与输入格式相同,系数保留一位小数;相邻两个输出数字之间需要有空格,输出最后没有空格;

解题思路:

定义三个float类型的数组,两个用于保存输入的多项式,一个用于保存输出的多项式;输入的奇数项是指数,偶数项是系数,所以可以对指数进行比较,如果相同则将指数和相加后的系数保存在第三个数组中,不同则将指数较大的项的指数和系数保存在第三个数组中;

  • 本题目采用C语言进行编写

int main() {

    int k1, k2, k3, count1 = 0, count2 = 0, count3 = 0;
    float NA1[20] = { 0 };
    float NA2[20] = { 0 };
    float NA3[40] = { 0 };
    char ch;

    scanf("%d", &k1);
    while (ch = getchar() != '\n')
    {
        scanf("%f", &NA1[count1++]);
    }

    scanf("%d", &k2);
    while (ch = getchar() != '\n')
    {
        scanf("%f", &NA2[count2++]);
    }

    count1 = count2 = 0;

    while (count1 < 2 * k1  && count2 < 2 * k2)
    {
        if (NA1[count1] == NA2[count2])
        {
            NA3[count3++] = NA1[count1++];
            count2 += 1;
            NA3[count3++] = NA1[count1++] + NA2[count2++];
        }
        else if (NA1[count1] > NA2[count2])
        {
            NA3[count3++] = NA1[count1++];
            NA3[count3++] = NA1[count1++];
        }
        else
        {
            NA3[count3++] = NA2[count2++];
            NA3[count3++] = NA2[count2++];
        }
    }
    while (count1 < 2 * k1) {
        NA3[count3++] = NA1[count1++];
        NA3[count3++] = NA1[count1++];
    }

    while (count2 < 2 * k2)
    {
        NA3[count3++] = NA2[count2++];
        NA3[count3++] = NA2[count2++];
    }

    k3 = (count3 + 1) / 2;

    for (int i = 0; i < count3; i++)
    {
        if (i % 2 == 0)
        {
            if (NA3[i + 1] == 0)
            {
                i++;
                k3--;
            }

        }
    }
    printf("%d", k3);
    for (int i = 0; i < count3; i++)
    {
        if (i % 2 == 0)
        {
            if (NA3[i + 1] == 0)
            {
                i++;
            }
            else
            {
                printf(" ");
                printf("%d", (int)NA3[i]);
            }
        }
        else {
            printf(" ");
            printf("%.1f", NA3[i]);
        }

    }
    system("pause");
    return 0;
}

总结:

在多项式相加中易被忽略的测试实例有:相加后的第一项为零,相加后最后一项为零,相加后整体为0;数据输出若系数为4,则应输出4.0才可通过测试;输出空格的技巧,除了项数不需要输出空格,其他空格都在数据输出之前;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值