PTA (Advanced Level) 1009 Product of Polynomials
1009 Product of Polynomials
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 (,) are the exponents and coefficients, respectively. It is given that 1, 0.
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
解题思路:
本题给出两个多项式要求计算并输出相乘后的多项式,第一行首先给出多项式A的项数K,之后跟随2K个数,即以指数 系数的方式给出A的每一项,第二行以与多项式A相同的方式给出多项式B的信息。
要求首先输出相乘后多项式的项数,之后按指数由小到大输出多项式的指数与系数
模拟运算过程,以三个double型的数组A、B、C记录给出的多项式与答案。
按要求输入多项式A,在输入多项式B时每输入一项便与A的每一项相乘,答案加入数组C中,遍历C统计非零项数量并输出,按下标由大到小输出C的非零项即可。
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6+;
//指数最大为1000,多项式相乘后的最大情况无非1000*1000
double A[MAX], B[MAX], C[MAX];
//A、B为给定多项式,C记录答案
int main()
{
int k, max_eA = INT_MIN, max_eB = INT_MIN;
scanf("%d", &k);
while(k--){ //输入多项式A
int en; //指数
double cn; //系数
scanf("%d%lf", &en, &cn);
A[en] = cn;
max_eA = max(max_eA, en); //记录多项式A的最大指数
}
scanf("%d", &k);
while(k--){ //输入多项式B
int en; //指数
double cn; //系数
scanf("%d%lf", &en, &cn);
B[en] = cn;
for(int i = max_eA; i >= ; i--)
C[i + en] += A[i] * B[en];//将该多项式B的项与多项式A的所有项对应相乘
max_eB = max(max_eB, en); //记录多项式B的最大指数
}
int cnt = ; //cnt记录答案C的非零项数
for(int i = max_eA + max_eB; i >= ; i--)
if(C[i] != 0.0)
cnt++;
printf("%d", cnt); //输出答案项数
for(int i = max_eA + max_eB; i >= ; i--) //输出答案多项式C
if(C[i] != 0.0)
printf(" %d %.1f", i, C[i]);
return ;
}
若第一个测试点(测试点0)不通过,多半是double类型运算时误差的锅(输入A、B完成后再两个for循环运算就可能会出现这种问题)。
PTA (Advanced Level) 1009 Product of Polynomials的更多相关文章
- PAT (Advanced Level) 1009. Product of Polynomials (25)
简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...
- 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 ...
- 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 pol ...
- 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)(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)
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 ...
- 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 ...
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
随机推荐
- Lombok自定义annotation扩展含Intellij插件
Lombok简介 Lombok(https://projectlombok.org/) 提供了以注解的形式为java对象增加属性和方法,这使得原来冗长的java源文件变的简洁(不需要再使用ide去生 ...
- Java的StringBuffer和StringBuilder类
StringBuffer (字符串缓冲对象) 概念:用于表示可以修改的字符串,称为字符串缓冲对象 作用:使用运算符的字符串将自动创建字符串缓冲对象 例如: str1+str2的操作,实际上是把str1 ...
- C++ 中的continue理解
continue的在循环中的作用: 1. 跳过当前循环,但是还需要执行自增条件, 如下程序:当i == 3时,执行i++, 即if判定{}执行完毕,则i==4, 然后 for最后一条语句i++, 然后 ...
- hdu 4941 map的使用
http://acm.hdu.edu.cn/showproblem.php?pid=4941 给定N,M和K,表示在一个N*M的棋盘上有K个棋子,给出K个棋子的位置和值,然后是Q次操作,对应的是: 1 ...
- js-数组方法push
<script type="text/javascript"> var arr=[1,2,3,4,5] arr.push(6,7) ...
- datatable fix error–Invalid JSON response
This error is pretty common. Meaning:When loading data by Ajax(ajax|option).DataTables by default, e ...
- [php-pear]如何使用 PHP-PEAR安装器,以及使用 PEAR 安装扩展库
我们都知道 PHP PEAR,就是 PHP Extension and Application Respository,也就是 PHP 扩展和应用代码库. PHP 也可以通过 PEAR 安装器来进行 ...
- iOS Document Interaction(预览和打开文档) 编程指南
原文:http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/DocumentInteracti ...
- 【BZOJ5306】 [Haoi2018]染色
BZOJ5306 [Haoi2018]染色 Solution xzz的博客 代码实现 #include<stdio.h> #include<stdlib.h> #include ...
- jzoj4223
考慮這樣一種暴力:將所有<=x的邊按照類似最小生成樹的方式加入答案,然後用下面的方法統計答案: 1.首先加入一條邊 2.看這條邊是否將會合成聯通塊,如果會,那麼加進這條邊,記這條邊一端聯通塊大小 ...