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 ...
随机推荐
- Eclipse workspace 被占用问题
eclipse 使用一段时间后,有时会因为一些故障自己就莫名奇妙的关闭了,再打开时有时没有问题,有时会提示错误 Workspace Unavailable: Workspace in use or c ...
- Mybatis类型转换介绍
1.1 目录 1.2 建立TypeHandler 1.2.1 TypeHandler接口 1.2.2 BaseTypeHandler抽象类 1.3 注册TypeHa ...
- mysql_触发器
mysql触发器 触发器:trigger,事先为某张表绑定好一段代码,当表中某些内容发生改变的时候(增删改),系统会自动触发代码,执行 触发器:事件类型,触发时间,触发对象 事件类型:增删改,三种类型 ...
- hdu2710 Max Factor
题目 //下面这个是最先用的方法,因为学姐先讲完这个,所以懒得写代码,就将就着这个用,结果搞了老半天,还是错了,心累.. #include<stdio.h> #include<str ...
- AngularJS 指令中的require
require参数可以被设置为字符串或数组,字符串代表另外一个指令的名字.require会将控制器注入到其值所指定的指令中,并作为当前指令的链接函数的第四个参数.字符串或数组元素的值是会在当前指令的作 ...
- [javascript-debug-ajax-json]两种不同的json格式数据
Bug 1: 1. 这里面的 data 只是一维数组{"state":0,"errorCode":0,"data":{"origi ...
- SQL Server--疑难杂症之坑爹的Windows故障转移群集
--============================================================== 估计是春节前最后一次写博客,也估计是本年值班最后一次踩雷,感叹下成也S ...
- 在 Ubuntu 16.04上安装 vsFTPd
在 Ubuntu 16.04上安装 vsFTPd Ubuntu vsFTPd 关于 vsFTPd vsFTPd 代表 Very Secure File Transfer Protocol Daemon ...
- WebAPI中发送字节数组
今天工作中遇到了一个情景: 前端向后台发送一个请求,希望后台返回一组数据,由于后台返回的数据量很大,希望尽可能压缩响应的大小 我的想法:后台将数据(Short的数组)直接转换成Byte[] 然后将b ...
- Duolingo 提高用户留存率的6个手段
翻译 :马玉洁 欢迎访问网易云社区,了解更多网易技术产品运营经验. 如果你用过"Duolingo"(Duolingo)这个语言教育应用程序,你就会知道它就像一款游戏. 这当然不是巧 ...