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 ...
随机推荐
- sqlliab7-8
less-7 https://www.jianshu.com/p/20d1282e6e1d ?id=0')) union select 1,'2','<?php @eval($_POST[&qu ...
- thinkphp5 -- _initialize()初始化控制器
public function _initialize() { parent::_initialize(); } public function __construct() { $; parent:: ...
- ajax后台返回指定的错误码
js: $.ajax({ type: "POST", url: 'post.php', data: serialNumber + "&getSerialNumbe ...
- 2019-2020-1 20199325《Linux内核原理与分析》第十一周作业
实验简介: Set-UID 是 Unix 系统中的一个重要的安全机制.当一个 Set-UID 程序运行的时候,它被假设为具有拥有者的权限.例如,如果程序的拥有者是root,那么任何人运行这个程序时都会 ...
- 2019-2020-1 20199328《Linux内核原理与分析》第十一周作业
预备实验部分 2019/11/27 10:17:34 下载安装后的界面,如图1 出现vulnerable字样,发现了shellshock漏洞,如图2 实验部分 2019/11/27 10:26:48 ...
- hdu_1050 Moving Tables 贪心
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Micropython教程之TPYBoard开发板驱动舵机教程(萝卜学科编程教育)
大家应该都看到过机器人的手臂啊腿脚啊什么的一抽一抽的在动弹吧...是不是和机械舞一样的有节奏,现在很多机器人模型里面的动力器件都是舵机. 但是大家一般见到的动力器件都是像步进电机,直流电机这一类的动力 ...
- postman的使用概览
本文主要描述postman的功能与使用方法Postman是404大厂的基于javascript语言完成的一款超级强大的插件,名字也很亲近(邮递员).可以用于做API请求测试.前端后台测试使用Postm ...
- 2019/2/20训练日记+map/multi map浅谈
Most crossword puzzle fans are used to anagrams - groups of words with the same letters in different ...
- 课堂管理系统;在线教辅平台;java课设
# 白泽智慧教辅平台 **说明:** 运行时需要修改位置: bzedu\src\main\resources\application.properties bzedu\src\main\resourc ...