题目

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 N​1​​ a​N​1​​ ​​ N​2​​ a​N​2​​ ​​ ... 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​​ <⋯<N​2 ​​ <N​1​​ ≤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

题目解析

给出两个多项式,每个输入格式是 非零项个数 指数1 系数1 指数2 系数2

让计算两多项式的 乘积,并按照指定格式输出 非零项个数 指数1 系数1 指数2 系数2

要求顺序是指数从高到低

思路

前面写过一个多项式求和的题 PAT 1002 A+B for Polynomials (25分),两个思想以及处理方式是一样的,只不过一个是加法,一个是乘法,区别就在于:加法,只有指数相同的项,系数才能相加;对于乘法,指数不同的两项,相乘以后得到一个新指数项(原指数相加,系数相乘)

  • 用一个数组来存储多项式,每一项的指数作为数组的索引系数作为,这样在读入时,直接找到对应位置进行修改,对数组的访问是很快的。
  • 没必要用两个数组把两个多项式都保存后再进行乘法运算,那样时间复杂度和空间复杂度都比较高,还要进行很多不必要的运算,我们就用一个数组初始化每一项都为0.0,相当于所有指数项系数都为0,读入第一个多项式时,根据指数系数改变对应位置的值即可读入第二个多项式时,每读入一个非零项,就用它分别和数组的每一项做运算,得到的结果应该加到下标为两指数相加的数组元素上。这样读入第二个多项式后,所有运算也做完了。
  • 之后一次遍历,统计出数组不为0的个数,就是非零项的个数;然后对数组从后往前输出每个非零项对应的下标和值,就是结果。注意精确到小数点后1

代码

#include <iostream>
using namespace std; int main() { // a,b是两个多项式,c是他们的乘积,指数作为下标,系数作为值,结果指数最高为2000
float a[1001] = {0.0}, c[2001] = {0.0}; int k; // 几个非零项
int exp; // 指数
float coe; // 系数 // 读入a
cin >> k;
while (k-- > 0) {
cin >> exp >> coe;
a[exp] = coe;
}
// 读入b的同时计算结果
cin >> k;
while (k-- > 0) {
cin >> exp >> coe;
// 这一项系数不为0,题目说了系数不为0
// if (coe != 0)
for (int j = 0; j < 1001; ++j)
// 指数相加,系数相乘
c[j + exp] += a[j] * coe;
}
// 统计结果非零项个数
int cnt = 0;
for (int i = 0; i < 2001; ++i)
if (c[i] != 0)
cnt++;
// 打印,末尾不能有多余空格,按指数从高到低,一位小数
cout << cnt;
for (int i = 2000; i >= 0; --i)
// 系数不为0才需要输出
if (c[i] != 0)
printf(" %d %.1f", i, c[i]); return 0;
}

PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值的更多相关文章

  1. 1009 Product of Polynomials (25分) 多项式乘法

    1009 Product of Polynomials (25分)   This time, you are supposed to find A×B where A and B are two po ...

  2. 【PAT甲级】1009 Product of Polynomials (25 分)

    题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...

  3. PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

  4. 1009 Product of Polynomials (25分) 晚上脑子就是容易僵住

    #include<iostream> using namespace std; struct { int a; double b; }poly[1001]; double a[2001]; ...

  5. PAT 甲级 1009 Product of Polynomials (25)(25 分)(坑比较多,a可能很大,a也有可能是负数,回头再看看)

    1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...

  6. PAT甲 1009. Product of Polynomials (25) 2016-09-09 23:02 96人阅读 评论(0) 收藏

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  7. pat 甲级 1009. Product of Polynomials (25)

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  8. PATA 1009. Product of Polynomials (25)

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  9. PAT 1009 Product of Polynomials

    1009 Product of Polynomials (25 分)   This time, you are supposed to find A×B where A and B are two p ...

随机推荐

  1. 进阶 Linux基本命令-2

    mkdir -p /a/b/c/d                 -p 循环创建目录yum install tree -y                                      ...

  2. 在Eclipse上实现简单的JDBC增删查改操作

    在Javaweb的学习里,学到了如何完成简单的增删查改操作,在这里撰写一篇文章以便自己整理回忆. 首先要建立一些包和导入一些文件.建一些类.具体框架如图  编写Product类 public clas ...

  3. Docker安装yapi

    安装docker 1.安装依赖包: yum install -y yum-utils device-mapper-persistent-data lvm2 2.安装 Yum -y install do ...

  4. 15个有趣好玩的linux shell 命令

    今天介绍一些有趣的linux shell命令,所有的命令都可以使用man + 命令名称 来查看完整的使用方法. 1,figlet 字符画 figlet 可以将英文字符串以字符画的形式输出: >& ...

  5. 【集群实战】共享存储实时备份(解决nfs共享存储的单点问题)

    1. nfs存储的单点问题 如果nfs服务器宕机了,则所有的nfs客户机都会受到影响.一旦宕机,会丢失部分用户的数据.为了解决单点问题,需要实现共享存储的实时备份,即:将nfs服务端共享目录下的数据实 ...

  6. APP路由还能这样玩

    本文主要讲述一种设计思路,组件化架构市面上已经有很多大厂成熟的方案,但是在组件化过程中,偶尔会遇到2个独立业务子模块间没有相互引用,也需要能直接调用对方的功能,因此我想到通过方法路由来解决,如果还有疑 ...

  7. 初见Ajax——javascript访问DOM的三种访问方式

    最近好啰嗦 最近在一间小公司实习,写一些小东西.小公司嘛,人们都说在小公司要什么都写的.果真是. 前端,后台,无论是HTML,CSS,JavaScript还是XML,Java,都要自己全包了.还好前台 ...

  8. 使用JXL.jar实现JAVA对EXCEL的读写操作

    简介: jxl.jar是通过java操作excel表格的工具类库 jxl操作excel包括对象Workbook(工作簿),Sheet(工作表) ,Cell(单元格). 一个excel就对应一个Work ...

  9. 图论--2-SAT--POJ Ikki's Story IV - Panda's Trick

    Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping ...

  10. ZABBIX自动发现Redis端口并监控

    由于一台服务器开启许多Redis实例,如果一台一台的监控太耗费时间,也非常容器出错.这种费力不讨好的事情我们是坚决杜绝的,幸好ZABBIX有自动发现功能,今天我们就来用该功能来监控我们的Redis实例 ...