使用STL求第K大数
来源:互联网 发布:centos搭建php网站 编辑:程序博客网 时间:2024/05/28 22:12
项目上需要使用C++,抽空正好把C++熟悉一下。
期待接下来的这个项目能给我的C++编程能力带来提升。
先来个set版本的。
#include <iostream>#include <string>#include <ios>#include <deque>#include <vector>#include <list>#include <set>#include <algorithm>using namespace std;int main( ) {inta, n, k, size;multiset<int> mySet;multiset<int>::iterator iter;cin >> n >> k;size = 0;do {cin >>a;if(size++ < k) {mySet.insert(a);} else {iter = mySet.begin();if(a > *iter) {mySet.erase(iter);mySet.insert(a);}}}while(--n );iter = mySet.begin();cout << *iter <<endl;system("pause");return 0;}再来个map版本的。
#include <iostream>#include <string>#include <ios>#include <deque>#include <vector>#include <list>#include <set>#include <map>#include <algorithm>using namespace std;int main( ) {inta, n, k, size;map <int, bool> myMap;map <int, bool>::iterator iter;cin >> n >> k;size = 0;do {cin >>a;if(size++ < k) {myMap[a] = true;} else {iter = myMap.begin();if(a > (iter->first)) { myMap.erase(iter);myMap[a] = true;}}}while(--n );iter = myMap.begin();cout << iter->first <<endl;system("pause");return 0;}
- 使用STL求第K大数
- 求第K大数[STL:nth_element]
- 求第k大数
- 求第k大数
- 求第k大数
- 求第k大数
- [HDU3292] 求第K大数
- 随机伪快排法 求第k大数
- 求数组中第K大数
- N个数,求第K大数
- 求数组的第k大数
- BFPRT算法求第k大数
- 求序列第K大数 POJ2104
- HDU 4006 求第k大数 treap
- N个数,求第K大数
- 快速排序与求第k大数
- N个数,求第K大数
- 堆的应用!--求第k大数
- 11级_Java_曹建波4.09 多态性&静态初始化块
- 什么是OTL
- oracle 动态SQL语句
- HTML5:使用postMessage实现Ajax跨域请求
- VelocityTracker简单用法
- 使用STL求第K大数
- linux下的time函数们
- 指定日期前一天的数据
- 使用window.name和postMessage实现js跨域
- g++编译优化指南
- 实现自己的string
- vs2010 创建dll
- ORA-12547经典错误处理
- 第八周任务三:实现分数类中的运算符重载