A1009. 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 (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
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int K, n, count = ;
double a, poly1[] = {}, re[] = {};
scanf("%d", &K);
for(int i = ; i < K; i++){
scanf("%d%lf", &n, &a);
poly1[n] = a;
}
scanf("%d", &K);
for(int i = ; i < K; i++){
scanf("%d%lf", &n, &a);
for(int j = ; j < ; j++)
re[n + j] = re[n + j] + poly1[j] * a;
}
for(int i = ; i >= ; i--){
if(re[i] != )
count++;
}
printf("%d", count);
for(int i = ; i >= ; i--){
if(re[i] != )
printf(" %d %.1lf", i, re[i]);
}
cin >> K;
return ;
}
总结:
1、指数为1000的多项式乘法,结果最高为2000次幂。
2、为减少时间复杂度,可以将第一个多项式存储,第二个不存。第二个多项式边读入边直接遍历poly1并做乘法。还可以将两个多项式的系数与指数分别开数组存下来以减小复杂度。
A1009. Product of Polynomials的更多相关文章
- A1009 Product of Polynomials (25)(25 分)
A1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are tw ...
- PAT A1009 Product of Polynomials (25 分)——浮点,结构体数组
This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...
- PAT甲级——A1009 Product of Polynomials
This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...
- PAT A1009 Product of Polynomials(25)
课本AC代码 #include <cstdio> struct Poly { int exp;//指数 double cof; } poly[1001];//第一个多项式 double a ...
- 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 ...
- PAT1009:Product of Polynomials
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 ...
- 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 polynomial ...
- PAT Product of Polynomials[一般]
1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...
随机推荐
- 想在已创建的Vue工程里引入vux组件
<1>. 在项目里安装vux npm install vux --save <2>. 安装vux-loader (这个vux文档似乎没介绍,当初没安装结果报了一堆错误) npm ...
- vue前端框架面试问题汇总
1.active-class是哪个组件的属性?嵌套路由怎么定义?答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么获取传过来的动态参数? 答: ...
- ssh 登陆服务器原理
这里分两种情况,这两种情况都涉及到公钥加密的概念. 由于公钥加密概念作为基础就不在本文进行讨论了. 使用ssh对远程服务器进行密码登录发生了什么: 客户端通过ssh连接服务器 1. 首先服务器把自己的 ...
- 从Oracle数据库中查询前几个月数据时需要注意的一些问题
在最近的一个项目中,有一个需求就是要查询数据库中前几个月的历史数据,但是由于自己考虑不全面造成了程序的bug,现在将这一块好好作一个总结,希望以后不再犯这种很低级的错误,首先贴出查询中用到的一个子函数 ...
- python数据结构与算法第十四天【二分查找】
1.二分查找的原理 对于已经排序的列表进行最快速度的查找 2. 代码实现 (1)递归实现 def binary_search(alist, item): if len(alist) == 0: ret ...
- vscode運行vue和html
html 选中html文件,右键选择view in broswer.
- How to sign app
codesign --display --verbose=4 /applications/qq.app codesign --display --entitlements - /application ...
- environment variable
%ALLUSERSPROFILE% C:\ProgramData %APPDATA% C:\Users\cuthead\AppData\Roaming %COMMONPROGRAMFILES% C:\ ...
- linux按时间查询日志
在系统应用集中部署的时候,很多日志因为太多难以定位,获取某段时间的日志是对运维人员非常关键的事情. 一.sed查看某时间段到现在的系统日志: sed -n '/May 20 17/,$p' / ...
- 官网下载旧版本jdk,老版本jdk,jdk1.7,jdk1.8
1.进入中文oracle官网(不是国内官网下载速度超级慢): http://www.oracle.com/technetwork/cn/indexes/downloads/index.html 2.进 ...