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 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 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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 <iostream>
#include <cstring>
#include <cstdio>
#include<map>
#include<stack>
#include<math.h>
using namespace std;
#define EPSION 0.1
map<int,double> mp;
map<int,double,greater<int> > mp2;
int main()
{
//freopen("1.txt","r",stdin);
int k,mi;
double xi;
scanf("%d",&k);
for(int i=;i<k;i++){
scanf("%d%lf",&mi,&xi);
mp[mi]=xi;
}
scanf("%d",&k);
int t;
double sz;
for(int i=;i<k;i++){
scanf("%d%lf",&mi,&xi);
//遍历,这样复杂度好高啊,但是感觉也只能这样了
for(map<int,double>::iterator it=mp.begin();it!=mp.end();it++){
t=it->first;
sz=it->second;
t+=mi;
sz*=xi; if(mp2.count(t)==)
mp2[t]=sz;
else
mp2[t]+=sz;
}
}
//接下来就是对map的关键字进行排序了。//自动的?
//printf("%d",mp2.size());//应该不能直接输出size
int ct=;
for(map<int,double>::iterator it=mp2.begin();it!=mp2.end();it++){
if(fabs(it->second)>=1e-)
ct++;
}
printf("%d",ct);
for(map<int,double>::iterator it=mp2.begin();it!=mp2.end();it++){
if(fabs(it->second)>=1e-)
printf(" %d %.1f",it->first,it->second);
}
return ;
}
//第一次提交出现了多种错误,格式错误和答案错误。。后来好几次提交都是20分。。本来以为会很简答,都不想上手写了。但是却搞了1.5h,真是醉了。
本题就是模拟多项式相乘。
1.多项式相乘可能系数会很小,这样计算机就不能识别,那么系数就为0.(也是因为这个一直提交只得了20分,不能直接输出mp2.size(),血的教训)
也就是说以后出现double相乘就要考虑是否会越最低界,
2.使用map时,它的关键字如果是可比较的,那么就会自动根据从小到大的顺序进行排序,如果需要反序,因为map默认的时less比较,那么就加上greater<int>就可以了,实现了map排序。
3.虽然说用数组可能会更简单,但是我觉得如果对map的操作比较熟悉了,也是一样的简单。
PAT Product of Polynomials[一般]的更多相关文章
- PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名
题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...
- 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 ...
- 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 ...
- PAT甲级——1009 Product of Polynomials
PATA1009 Product of Polynomials Output Specification: For each test case you should output the produ ...
- PAT1009:Product of Polynomials
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
- 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 ...
随机推荐
- spring mvc 跨域请求处理——spring 4.2 以上
Controller method CORS configuration You can add to your @RequestMapping annotated handler method a ...
- Python汉字转换成拼音
最近在使用Python做项目时,需要将汉字转化成对应的拼音. 网上的一些包大多是python2.x的,使用下面这个包,支持python3.6 xpinyin 0.5.5 >>> fr ...
- nginx(一)----ubuntu14.04下安装nginx
/** * lihaibo * 文章内容都是根据自己工作情况实践得出. *如有错误,请指正 *转载请注明出处 */ 此文章中用到的软件下载地址: 链接: http://pan.baidu.com/s/ ...
- 配置Mac漂亮的Shell--Iterm2+OhMyZSH+Agnoster
安装包管理器 首先当然是解决包管理的问题,Mac下面是Homebrew的天下了 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubuserco ...
- 【BZOJ2658】[Zjoi2012]小蓝的好友(mrx) 平衡树维护笛卡尔树+扫描线
[BZOJ2658][Zjoi2012]小蓝的好友(mrx) Description 终于到达了这次选拔赛的最后一题,想必你已经厌倦了小蓝和小白的故事,为了回馈各位比赛选手,此题的主角是贯穿这次比赛的 ...
- jenkins使用Publish Over SSH中遇到的问题
在jenkins中想使用publish over ssh来在构建后发送XML或PNG文件至服务器,以便做其它的操作,安装完publish over ssh后,填加构建,发现在构建失败时,不传送文件,老 ...
- Unity3D笔记 英保通六 角色控制器
一.角色控制器 U3D有两种角色控制方式:Rigidbody刚体.角色控制器组件(胶囊体组件) 面试的题目中经常会遇到这个问题: CharacterController和Rigidbody的区别? 这 ...
- Edge Animate初篇教程(二)
Edge Animate 是Adobe最新出品的制作HTML5动画的可视化工具,简单的可以理解为HTML5版本的Flash Pro.在之后的文章中,我会逐一的介绍这款新的HTML5动画神器. 一.创建 ...
- Java8新特性之Lambda表达式
lambda表达式是java8给我们带来的几个重量级新特性之一,借用lambda表达式,可以让我们的java程序设计更加简洁.最近新的项目摒弃了1.6的版本,全面基于java8进行开发,本文是java ...
- Oracle创建测试表
试中文排序的数据库版本: SQL> select * from v$version; BANNER ----------------------------------------------- ...