[ PAT-A ] 1009 Product of Polynomials(C++)

该博客介绍了一个PAT-A编程题目,要求计算两个多项式的乘积。输入包括两个多项式,每个多项式由非零项的个数和对应指数及系数组成。输出为相乘结果的多项式,指数相加,系数相乘。博客强调了指数相同的系数相加后为0的情况以及结果系数需精确到小数点后一位,并提供了输入模块和两多项式相乘的实现思路。此外,博主分享了在牛客网上处理数据保留一位小数的困扰及解决代码。
摘要由CSDN通过智能技术生成
题目描述

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~N1~ N2 a~N2~ … NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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


解题思路

题目大意为两多项式相乘得出一个新的多项式,设计思路很简单,指数相加,系数相乘.需要注意的地方有:

  • 同指数的系数相加后为0,不可以再看作该多项式的一个单项式
  • 对于系数,应当精确到小数点后一位(PTA网站上的数据对这个没有严格要求,但牛客网要求比较严格)

代码设计

1.输入模块

[任务]
存放一个多项式
[说明]
如果有多项式的话,将会被存放到map中去
[接口]
bool input(map<double,double>& m)

bool input(map<double,double>& m)
{
    int n;double ex,co;
    if(scanf("%d",&n)!=1) return false;
    for(int i=0;i<n;i++){cin>>ex>>co;m[ex]=co;}
    return true;
}

2.两多项式相乘模块

[任务]
将给定的两多项式相乘
[说明]
指数相加,系数相乘.对运算后的多项式化简时,同指数的系数进行相加
[接口]
void op(map<double,double> a,map<double,double> b)

void op(map<double,double> a,map<double,double> b)
{
    for(map<double,double>::iterator ita=a.begin();ita!=a.end();ita++)
        for(map<double,double>::iterator itb=b.begin();itb!=b.end();itb++)
            re[ita->first+itb->first]+=ita->second*itb->second;
}

额外话:牛客网上对数据保留一位小数的处理上,一直不得其解
牛客网题目链接
牛客网WA的代码
牛客网AC的代码



有关PAT-B的更多内容可以关注 ——> PAT-B题解

铺子日常更新,如有错误请指正
传送门:代码链接 题目链接 PAT-A题解

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值