PAT-Apat甲级题1009(python和c++实现)

PTA | 1009 Product of Polynomials

1009 Product of Polynomials

作者 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 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 product 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 up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6

万事开头难,先读题!!!

这一次,你应该找到A×B,其中A和B是两个多项式。
输入规范:

每个输入文件包含一个测试用例。每种情况占用2行,每行包含一个多项式的信息:

K N1 aN1 N2 aN2. NK aNK

其中K是多项式中非零项的数目,Ni和aNi(i= 1,2,k,K)分别是指数和系数。给出了1≤K≤10,0≤NK <N2 <N1 ≤1000。
输出规格:

对于每个测试用例,您应该在一行中输出A和B的乘积,并使用与输入相同的格式。请注意,每行末尾必须没有多余的空格。请精确到小数点后1位。

完整的一遍题目读下来,可以发现,这个题目和上次的多项式求和很像很像,只是这边变成了多项式的乘法,那么其实大部分的解题思路还是完全一样的,那么从上述题目中,我们可以提取得到以下信息:

        1, 输入包含两个部分,一个部分是非零项数的个数,接下来是各个非零项指数及其次数

        2, 输出格式和输入一致,并且要求多项式系数部分保留一位小数

归纳总结完题目要点之后,现在是手搓代码时间!!

首先老样子,我们定义接收输入变量,先对输入进行处理,由于输入包含两次的内容,可以使用循环的方法进行存储,但是后续访问操作会变得比较困难,这里就直接偷懒,直接复制一份一摸一样的处理内容,在处理完输入之后,我们需要考虑如何处理存储计算结果的问题,那么这里结合C++和python这两种语言的特性,c++直接定义足够大的double数组,python则根据两次输入的最高项求和,以此初始化大小合适的dict,由于题中给出的最大的项数是1000项,这里初始化C++的double数组大小为2001,本部分代码如下:

C++:

    double D[2001] = {0};
    int k1, a1[21];
    double d1[21];
    cin >> k1;
    for(int i=0;i<k1;i++){
        cin>> a1[i] >> d1[i];
    }
    int k2, a2[21];
    double d2[21];
    cin >> k2;
    for(int i=0;i<k2;i++){
        cin>> a2[i] >> d2[i];
    }

python:

为了方便后续操作,这里直接对输入数据进行提取,将”指数:系数“关系对提取出来,并初始化大小为两次输入最高项之和的dict用于存储计算结果match_

lst1 = [eval(i) for i in input().split()][1:]
lst2 = [eval(i) for i in input().split()][1:]
mat1 = {lst1[i]:lst1[i+1] for i in range(0, len(lst1), 2)}
mat2 = {lst2[i]:lst2[i+1] for i in range(0, len(lst2), 2)}
match_ = {i:0 for i in range(lst1[0]+lst2[0]+1)}

此后就是分别遍历两次的输入内容,对其进行多项式的乘法操作,对于存储,python中,只要两个多项式相乘的结果(系数相乘,指数相加)中,指数对应上match_中的key,就将其value和计算结果相加,对于C++,由于初始化的为数组,则将数组下标作为指数运算的结果,将该下标对应的元素与系数运算的结果相加,同时,由于初始化中,将所有的指数情况都进行考虑,并将其初始结果赋初值为0,则需要另外统计运算结果的非零项,统计结果部分其实也较为简单,定义记录非零项的变量nozero,直接暴力遍历多项式运算结果,只要其系数部分不为零,则统计结果++,本部分代码如下:

C++:

    for (int i=0;i<k1;i++){
        for(int j=0;j<k2;j++){
            D[a1[i]+a2[j]] += d1[i] * d2[j];
        }
    }
    int nozero = 0;
    for(int i=0;i<2001;i++){
        if(D[i] != 0)nozero += 1;
    }

python:

for i in mat1:
    for j in mat2:
        match_[i+j] += mat1[i] * mat2[j]
nozero = 0
for i in match_:
    if match_[i] != 0:
        nozero += 1
print(nozero, end="")

对于最后的输出部分,由于要求较为简单,这里将不再赘述,但是涉及格式化输出的部分,还是建议使用最为原始的%d,%.1f这种格式。

以下是完整的代码:

python:

lst1 = [eval(i) for i in input().split()][1:]
lst2 = [eval(i) for i in input().split()][1:]
mat1 = {lst1[i]:lst1[i+1] for i in range(0, len(lst1), 2)}
mat2 = {lst2[i]:lst2[i+1] for i in range(0, len(lst2), 2)}
match_ = {i:0 for i in range(lst1[0]+lst2[0]+1)}
for i in mat1:
    for j in mat2:
        match_[i+j] += mat1[i] * mat2[j]
nozero = 0
for i in match_:
    if match_[i] != 0:
        nozero += 1
print(nozero, end="")
res = sorted(match_.items(),key=lambda x:x[0], reverse=True)
for i in res:
    if i[1] != 0:
        print(" %d %.1f" % (i[0], i[1]),end="")

C++:


#include<bits/stdc++.h>
using namespace std;

int main(){
    double D[2001] = {0};
    int k1, a1[21];
    double d1[21];
    cin >> k1;
    for(int i=0;i<k1;i++){
        cin>> a1[i] >> d1[i];
    }
    int k2, a2[21];
    double d2[21];
    cin >> k2;
    for(int i=0;i<k2;i++){
        cin>> a2[i] >> d2[i];
    }
    for (int i=0;i<k1;i++){
        for(int j=0;j<k2;j++){
            D[a1[i]+a2[j]] += d1[i] * d2[j];
        }
    }
    int nozero = 0;
    for(int i=0;i<2001;i++){
        if(D[i] != 0)nozero += 1;
    }
    cout << nozero;
    for(int i=2000;i>=0;i--){
        if(D[i] != 0){
            printf(" %d %.1f", i, D[i]);
        }
    }
    return 0;
}

最后附上AK截图:

python:

C++:

写在后面:

        对于本题,由于前面出现过类似的代码题目,总体来说理解起来还是比较简单,本题采用的方法比较直接了当,需要稍微注意一些细节问题,如果您还有更好的解题思路或者方法,欢迎评论区交流!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值