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 ...
随机推荐
- 【cs229-Lecture8】顺序最小优化算法
ref: blog:http://zhihaozhang.github.io/2014/05/20/svm4/ <数据挖掘导论> 真正的大神是当采用的算法表现出不是非常好的性能的时候 ...
- c++ 类内部函数调用虚函数
做项目的过程中,碰到一个问题. 问题可以抽象为下面的问题: 普通人吃饭拿筷子,小孩吃饭拿勺子. class People { public: void eat() { get_util_to_eat( ...
- Esper学习之八:EPL语法(四)
关于EPL,已经写了三篇了,预估计了一下,除了今天这篇,后面还有5篇左右.大家可别嫌多,官方的文档对EPL的讲解有将近140页,我已经尽量将废话都干掉了,再配合我附上的例子,看我的10篇文章比那140 ...
- 二叉树的实现(Java语言描述)
实现二叉树 并先序遍历之. package 二叉树的实现; public class BinaryTree<T> { class Node { int value; // 该节点存储的 ...
- 原生js(三)
客户端js的时间线: 1.web浏览器创建Document对象,开始解析html和文本.生成Element对象和Text节点添加到文档中.这个阶段的document.readystate==" ...
- Android NDK学习(2)Windows下NDK开发环境配置
转:http://www.cnblogs.com/fww330666557/archive/2012/12/14/2817386.html 一.配置好Android开发环境 二.下载安装安卓NDK ...
- Sencha Touch 实战开发培训 视频教程 第二期 第一节
经过忙碌的准备,终于在2014.4.7晚上8:10分开课. 本来预定在8点开课的,不过电脑出了点问题,推迟了. 本期培训一共八节,前两节免费,后面的课程需要付费才可以观看. 本节内容: 了解Sench ...
- FPM打包工具使用
author:headsen chen date: 2019-01-19 14:57:09 个人原创博客,转自请注明出处和作者,否则追究法律责任 1,安装依赖和语言包 yum -y install ...
- 【转】C内存管理
在任何程序设计环境及语言中,内存管理都十分重要.在目前的计算机系统或嵌入式系统中,内存资源仍然是有限的.因此在程序设计中,有效地管理内存资源是程序员首先考虑的问题. 第1节主要介绍内存管理基本概念,重 ...
- 【BZOJ4036】[HAOI2015]按位或 FWT
[BZOJ4036][HAOI2015]按位或 Description 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行或(c++,c的|,pascal的or ...