L1-027 出租(20)(STL-map代码)
L1-027 出租(20 分)
下面是新浪微博上曾经很火的一张图:
一时间网上一片求救声,急问这个怎么破。其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2]=1,index[1]=0 对应 arr[0]=8,index[2]=3 对应 arr[3]=0,以此类推…… 很容易得到电话号码是18013820100。
本题要求你编写一个程序,为任何一个电话号码生成这段代码 —— 事实上,只要生成最前面两行就可以了,后面内容是不变的。
输入格式:
输入在一行中给出一个由11位数字组成的手机号码。
输出格式:
为输入的号码生成代码的前两行,其中arr中的数字必须按递减顺序给出。
输入样例:
18013820100
输出样例:
int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};
#include<iostream>
#include<algorithm>
#include<string>
#include<functional> //greater头文件
#include<map>
using namespace std;
int main() {
map<char, int, greater<int>> arr; //加密“字典”,降序排列
string str;
cin >> str;
for (int i = 0; i < str.length(); i++)
arr[str[i]] = 0;
cout << "int[] arr = new int[]{";
int n = 0;
for (auto it = arr.begin(); it != arr.end(); it++) {
cout << it->first;
if (it != --arr.end())
cout << ",";
it->second = n++;
}
cout << "};" << endl;
cout << "int[] index = new int[]{";
for (int i = 0; i < str.length(); i++) {
cout << arr[str[i]];
if (i != str.length() - 1)
cout << ",";
}
cout << "};" << endl;
return 0;
}
L1-027 出租(20)(STL-map代码)的更多相关文章
- LeetCode 1. Two Sum (c++ stl map)
题目:https://leetcode.com/problems/two-sum/description/ stl map代码: class Solution { public: vector< ...
- STL MAP及字典树在关键字统计中的性能分析
转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...
- stl::map之const函数访问
如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- 泛型Binary Search Tree实现,And和STL map比较的经营业绩
问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...
- STL map 简介
STL map 简介 转载于:http://www.cnblogs.com/TianFang/archive/2006/12/30/607859.html 1.目录 map简介 map的功能 使用ma ...
- C++ - STL - map的基础操作
STL - map常用方法 map简述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,其作用类似于 ...
- Features Track[STL map]
目录 题目地址 题干 代码和解释 参考 题目地址 Features Track(ACM-ICPC 2018 徐州赛区网络预赛 ) 题干 代码和解释 题意:一个动画有许多 n 帧,每帧有 k 个点,点的 ...
- C++ STL map容器值为指针时怎么释放内存
最近在使用STL中map时,遇到了一个问题,就是当map中值为指针对象时怎么释放内存? // 站点与TCP连接映射表 (key为ip_port_stationCode, value为 clientSo ...
- 20行Python代码爬取王者荣耀全英雄皮肤
引言王者荣耀大家都玩过吧,没玩过的也应该听说过,作为时下最火的手机MOBA游戏,咳咳,好像跑题了.我们今天的重点是爬取王者荣耀所有英雄的所有皮肤,而且仅仅使用20行Python代码即可完成. 准备工作 ...
随机推荐
- python递归、collections系列以及文件操作进阶
global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaults log global mode http t ...
- SQL调用C# dll(第二中DLL,强名称密匙)
参考:微软官网 https://msdn.microsoft.com/zh-cn/library/ms345106(es-es).aspx 1.新建项目 SQLDllTestUsingNew Clas ...
- iOS Dev (25) 解决“The executable was signed with invalid entitlements.”问题
2014-01-10 10:34 5240人阅读 评论(1) 收藏 举报 目录(?)[+] iOS Dev (25) 解决“The executable was signed with inv ...
- Python 测试
(1)import doctest doctest.testmod(verbose=True) (2) pip install tests
- java.lang.IllegalAccessError: tried to access method
java.lang.IllegalAccessException: access to method denied 06-23 16:12:39.128 1253-1253/com.hbjyjt.oa ...
- 启用mongodb授权认证
linux下 1.以–auth 启动mongod 2.在配置文件mongod.conf 中加入 auth = true
- Gimbal Lock
[Gimbal Lock] 万向锁源于欧拉角的是有序处理的.U3D中的序列为: y->x->z.当旋转y时,local坐标系与世界坐标系重合,所以y等于永远按惯性坐标旋转.当x旋转+/-9 ...
- yum被锁定:Another app is currently holding the yum lock; waiting for it to exit…
yum被锁定无法使用,错误信息截图如下: 解决方法:rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了
- 【OpenGL】三角形
步骤 初始化顶点数组对象VAO 分配顶点缓冲对象VBO 将顶点数据载入缓冲对象中 glBufferData() 链接顶点属性 glVertexAttribPointer(指定了顶点着色器的变量与我们存 ...
- windows 查询端口占用 杀掉进程
参考 https://www.cnblogs.com/lynn-li/p/6077993.html netstat -ano | findstr "8001" taskkill / ...