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 ...
随机推荐
- orcale 多列转一行显示
强大的数据库有个自带函数wm_concat() wm_concat()这个函数放的是需要汇总的列 select wm_concat(name) name from tablename
- Scala学习系列(三)——入门与基础
本课程源码共享于 https://github.com/tree1123/learning-scala 首先,打开IDEA编辑器的SbtExampleProject项目,我们将在这个项目下进行练习 本 ...
- Springboot整合https原来这么简单
1 简介 HTTP是不安全的,我们需要给它套上SSL,让它变成HTTPS.本文章将用实例介绍Springboot整合HTTPS. 2 密码学基础 要谈https就要谈Security,自然就要谈安全: ...
- 数据库 MySQL 练习
一.sql语句基础 1.顯示德國 Germany 的人口 SELECT population FROM world WHERE name = 'Germany' 2.查詢面積為 5,000,000 ...
- java stream中Collectors的用法
目录 简介 Collectors.toList() Collectors.toSet() Collectors.toCollection() Collectors.toMap() Collectors ...
- 理解分布式一致性:Paxos协议之Basic Paxos
理解分布式一致性:Paxos协议之Basic Paxos 角色 Proposal Number & Agreed Value Basic Paxos Basic Paxos without f ...
- mac OS 安装 Subversion JavaHL 客户端
JavaHL原来官网 http://subclipse.tigris.org/wiki/JavaHL 目前已经全部转移到github 官方说明很详细 https://github.com/subcl ...
- 桌面上的Flutter:Electron又多了个对手
从本质上看,Flutter是一个独立的二进制可执行文件.它不仅改变了移动设备的玩法,在桌面设备上也同样不可小觑.一次编写,可在Android.iOS.Windows.Mac和Linux上进行原生部署, ...
- CF1328E Tree Queries
CF1328E Tree Queries 应该还是比较妙的 题意 给你一个树,然后多次询问 每次询问给出一堆节点,问你是否能找到一个从根出发的链,是的对于给出的每个节点,都能找出链上的点,是的他们的距 ...
- xenomai内核解析之xenomai的组成结构
@ 目录 一.xenomai 3 二.xenomai3 结构 这是第二篇笔记. 一.xenomai 3 从xenomai3开始支持两种方式构建linux实时系统,分别是cobalt 和 mercury ...