关于STL模板的使用效率与控制输出时最后不加空格问题

题目:PAT 1009
149ms vs 7ms的写法的细微区别

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

简单说,题目就是求解两个多项式的乘积。

中心思想是:哈希映射,以空间换时间。

关于多项式相乘,用的是二层循环计算。同一个指数的系数,比如1+3 = 2+2, 用映射就可以解决问题。

7ms计算完毕版本:

#include <stdio.h>
#include <vector>
#include <map>using namespace std;float polA[1001] = {0.0};
float polB[1001] = {0.0};
float polC[2001] = {0.0};
int main()
{int m,n;scanf("%d ",&m);int exponent;float coefficient;for(int i = 0; i < m; i++) // process input{scanf("%d %f", &exponent, &coefficient);polA[exponent] = coefficient;}scanf("%d ",&n);for(int i = 0; i < n; i++) // process input{scanf("%d %f", &exponent, &coefficient);polB[exponent] = coefficient;}for(int i = 0; i < 1001; i++) // calcuate{for(int j = 0; j < 1001; j++){polC[i + j] += polA[i] * polB[j];}}int cnt = 0;for(int i = 0; i < 2001; i++){if(polC[i]) cnt++;}printf("%d",cnt);for(int i = 2000; i >=0; i--){if(polC[i]){printf(" %d %.1f", i,polC[i]);}}printf("\n");return 0;
}

149ms计算完毕版本:

#include <stdio.h>
#include <vector>
#include <map>using namespace std;map<int,float> polA;
map<int,float> polB;
map<int,float> polC;
int main()
{//initfor(int i = 0; i < 2001; i++){polA.insert(pair<int, float>(0,0));polB.insert(pair<int, float>(0,0));polC.insert(pair<int, float>(0,0));}int m,n;scanf("%d ",&m);int exponent;float coefficient;for(int i = 0; i < m; i++) // process input{scanf("%d %f", &exponent, &coefficient);polA[exponent] = coefficient;}scanf("%d ",&n);for(int i = 0; i < n; i++) // process input{scanf("%d %f", &exponent, &coefficient);polB[exponent] = coefficient;}for(int i = 0; i < 1001; i++) // calcuate{for(int j = 0; j < 1001; j++){polC[i + j] += polA[i] * polB[j];}}int cnt = 0;for(int i = 0; i < 2001; i++){if(polC[i]) cnt++;}printf("%d",cnt);for(int i = 2000; i >=0; i--){if(polC[i]){printf(" %d %.1f", i,polC[i]);}}printf("\n");return 0;
}

对比两个版本可以看到,使用map的哈希法更慢。而在本题的算法中,直接用数组构造映射,则会快得多。

启示是:简单的基本类型的映射,最佳的选择是使用数组来模拟。

END.

关于STL模板的使用效率与控制输出时最后不加空格问题相关推荐

  1. C++ STL模板库用法查询及一些常见面试题(自用)

    C++STL模板库 文章目录 C++STL模板库 用法查询 Array 1.使用 2.创建 3.成员函数 Vector 1.使用 2. 创建 3.成员函数 deque 1.使用 2. 创建 3.成员函 ...

  2. c++ 的 stl模板库_C ++中的标准模板库(STL)

    c++ 的 stl模板库 Standard Template Library (STL) is a collection of standard C++ template classes. It co ...

  3. STL模板之vector与sort的使用

    STL模板之vector与sort的使用 @(算法学习) 题目描述 题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩.都按先录入排列在前的规则处理. 例示: jack ...

  4. 运算符重载、STL模板库、类型转换、异常处理机制(C++五)

    目录 一.运算符重载 1. 引入运算符重载 2.语法格式 3.运算符重载有两种表现形式 4.运算符重载分类 5.运算符重载的特点: 二.C++中的STL模板库的使用 1.容器类(container) ...

  5. Hadoop控制输出文件命名

    参考:http://blog.csdn.net/zuochanxiaoheshang/article/details/8769198 Hadoop 控制输出文件命名 在一般情况下,Hadoop 每一个 ...

  6. STM32F4 定时器TIM(1)定时器控制输出【使用库函数】

    高级时钟控制定时器TIM1&TIM8简介: STM32F4的高级控制定时器包含一个自动重装载计数器,计数器的输入是一个被预分频的系统时钟. 这个定时器有多种用途,包括车辆输入信号长度(输入捕获 ...

  7. c语言控制输出间隔大小,C/C++ 输出间隔控制

    C++使用setw(int n)来控制他后面的变量的输出占多少个位置.默认是右对齐. 例子: #include using namespace std; #include using std::set ...

  8. SparkSql 控制输出文件数量且大小均匀(distribute by rand())

    @羲凡--只为了更好的活着 SparkSql 控制输出文件数量且大小均匀(distribute by rand()) Q:Spark如何控制文件你输出数量? A:这个简单,用 coalesce或者re ...

  9. 按键控制输出占空比可调的PWM波

    这里写自定义目录标题 按键的误区 举个例子 代码如下 实验结果 如何插入一段漂亮的代码片 生成一个适合你的列表 创建一个表格 设定内容居中.居左.居右 SmartyPants 创建一个自定义列表 如何 ...

最新文章

  1. 为什么需要在TypedArray后调用recycle
  2. 【ICCV-2019】ACNet:通过非对称卷积块增强CNN的核骨架 3*3卷积==>1*3卷积+3*1卷积=白给的精度提升
  3. (Linux 日常命令)[20171225]
  4. flexbox布局_Flexbox vs Grid-如何构建最常见HTML布局
  5. Chapter 14 Exercises Problems
  6. OC 应用跳转QQ私聊界面或者申请加群
  7. ipad远程连接虚拟机linux,如何从ipad pro上通过SSH远程Linux
  8. [原创]写给自己的总结—2014到2015
  9. numpy 中的 broadcasting(广播)机制
  10. ajax通用代码,通用代码之一: 轻松使用Ajax
  11. 7-1 计算物体自由下落的距离 (5 分)
  12. shardedJedisPool工具类
  13. BOOST库介绍(二)——BOOST多线程相关库
  14. PlatformIO for CLion 创建工程时出现 gathering information问题解决方法
  15. matlab机器学习基础
  16. MICRO‘21文章挑选(感兴趣)
  17. “智慧银行”行业研究报告要点整理
  18. php生成word,并下载
  19. linux(ubuntu)系统什么叫:桌面管理器,窗口管理器?
  20. 超说网络NO.4 | 深入了解应用层原理(中科大 郑烇)

热门文章

  1. 值传递,地址传递,引用传递
  2. 简单的android ant 批量打包
  3. Android 工程师进阶手册(8 年 Android 开发者的成长感悟)
  4. 设计师怎么优雅的薅羊毛——技能get!!
  5. 安装vscode,下载很慢的解决
  6. java腾讯滤镜接口_腾讯内容开放平台
  7. myeclipse-添加jar包
  8. Foehn Phenomena
  9. RS-232或RS-485与CAN网络互联互通
  10. cad考试题库绘图题答案_CAD考试试题库及参考答案