PAT 1009 Product of 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 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
题目解析
给出两个多项式,每个输入格式是 非零项个数 指数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分) 指数做数组下标,系数做值的更多相关文章
- 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 ...
- 【PAT甲级】1009 Product of Polynomials (25 分)
题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...
- 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 ...
- 1009 Product of Polynomials (25分) 晚上脑子就是容易僵住
#include<iostream> using namespace std; struct { int a; double b; }poly[1001]; double a[2001]; ...
- 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 ...
- 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 ...
- pat 甲级 1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PATA 1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
随机推荐
- js上传文件前判断获取文件大小并且加以判断
描述:要求浏览器单个上传文件大小不超过10M. 解决方案: var fileSize = $("#fileId")[0].files[0].size/(1024*1024);if( ...
- python学习笔记(二)---for循环与操作列表
内容概要 for循环 range(start,end,step)函数 生成随机数列表 list()函数 将range()的结果整合到某个列表 列表的操作 切片(start: end :step) 元组 ...
- ajax发送时禁用按钮
css /*div无法点击*/ .div-cant-click { pointer-events: none; } 发送ajax实例 $.ajax({ url:'url', type:'post', ...
- Metasploit渗透测试环境搭建
渗透测试实验环境搭建 下载虚拟机镜像 5个虚拟机镜像,其中Linux攻击机我选择用最新的kali Linux镜像,其余的均使用本书配套的镜像. 网络环境配置 VMware虚拟网络编辑器配置: 将VMn ...
- Redis持久化存储(二)
redis多实例介绍 接上一篇redis.创建数据存放的目录 vim redis.conf +187 dir /application/data/ 重新启动 mkdir /application/da ...
- 数据挖掘入门系列教程(十二)之使用keras构建CNN网络识别CIFAR10
简介 在上一篇博客:数据挖掘入门系列教程(十一点五)之CNN网络介绍中,介绍了CNN的工作原理和工作流程,在这一篇博客,将具体的使用代码来说明如何使用keras构建一个CNN网络来对CIFAR-10数 ...
- MATLAB学习1 之画图函数
ezplot适用条件 "ezplot"命令可以用于显函数.隐函数和参数方程作图. 不同函数的使用格式 显函数y=f(x),ezplot函数的调用格式为ezplot(f, [xmin ...
- HDU 4009 Transfer water(最小树形图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...
- 虚拟化VMware之虚拟机备份(1)
之虚拟机备份() 模版:是一种开放,公用.安全的虚拟机压缩格式,通常使用的是扩展名为.ova可以在多个主流虚拟化平台下进行操作 是和 通过技术协作推出的基于磁盘的备份和恢复的新一代解决方案,可靠且易部 ...
- UVA352 The Seasonal War
本文为UserUnknown原创 题目本身不难理解,就是深搜(或广搜,有可能以后会加在这里). 但是洛谷的题目中没有截到输入输出的格式,下面是我从UVA复制下来的样例: Sample input 6 ...