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 ...
随机推荐
- 【Linux】Could not resolve: www.test.com (Could not contact DNS servers)
在请求微信小程序服务时候报错了 从这个报错,可以很明显的发现是域名解析不了 1 故障排查:因为代码里调用的是curl,所以测试一下curl是否能够正常解析dns 果然不行, 2 解决办法: vi /e ...
- python爬虫---->scrapy的使用(一)
这里我们介绍一下python的分布式爬虫框架scrapy的安装以及使用.平庸这东西犹如白衬衣上的污痕,一旦染上便永远洗不掉,无可挽回. scrapy的安装使用 我的电脑环境是win10,64位的.py ...
- C++中的异常安全性【转】
原文写的非常好,来自这里 一个函数如果说是“异常安全”的,必须同时满足以下两个条件:1.不泄漏任何资源:2.不允许破坏数据. 我们先通过两个反面的例子开始. 第一个是造成资源泄漏的例子.一个类Type ...
- 跟bWAPP学WEB安全(PHP代码)--SQL注入的一些技巧
背景 模拟环境还是 bWAPP,只不过这个bWAPP的SQL注入有点多,一一写意义不大,在这边就利用这个环境来尝试一些SQL注入的技巧.并研究下PHP的防御代码. 普通的bWAPPSQL注入的简单介绍 ...
- 【BZOJ1187】[HNOI2007]神奇游乐园 插头DP
[BZOJ1187][HNOI2007]神奇游乐园 Description 经历了一段艰辛的旅程后,主人公小P乘坐飞艇返回.在返回的途中,小P发现在漫无边际的沙漠中,有一块狭长的绿地特别显眼.往下仔细 ...
- python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制
一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这 ...
- angular开发控制器之间的通信
一.指令与控制器之间通信,无非是以下几种方法: 基于scope继承的方式 基于event传播的方式 service的方式(单例模式) 二.基于scope继承的方式: 最简单的让控制器之间进行通信的方法 ...
- iOS - 使用苹果自带的UIVideoEditController进行视频编辑
UIVideoEditorController类包含了由系统提供的界面,使用户可以交互式的剪切视频.UIVideoEditorController对象处理用户的交互并且提供把编辑后的视频的文件系统路径 ...
- 关于IP地址子网的划分
- PAT-GPLT L2-027 - 名人堂与代金券 - [简单模拟]
题目链接:https://www.patest.cn/contests/gplt/L2-027 对于在中国大学MOOC(http://www.icourse163.org/)学习“数据结构”课程的学生 ...