01串的排序问题_算法
2010-09-23 11:50
225 查看
01 串的排序问题:
Code:
//首先按长度排序,
//长度一样,按'1'的个数排序,
//‘1’的个数一样时,就按ASCII排序
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <fstream>
//if there is not included fstream libary ,we will get the bottom error:
///main.cpp|26|error: variable `std::ifstream cin' has initializer but incomplete type|
//||=== 完成的Build: 1 个错误, 0 个警告 ===|
using namespace std;
struct cmp//比较算法:从'1'的个数 按小到大排
{
//重载()操作
bool operator () (const string &s1,const string &s2)//返回1 ,0 ,-1
{
if(s1.length()!=s2.length())
return s1.length()<s2.length(); //返回比较长度的逻辑值
int c1 = count(s1.begin(),s1.end(),'1');
int c2 = count(s2.begin(),s2.end(),'1');
return (c1!=c2?c1<c2:s1<s2); //c1等于c2时返回c1<c2的逻辑值,否者返回ASCII值
}
};
int main(int argc,char *argv[])
{
multiset<string,cmp>ms;//Why choose multiset data struct in this place?
ifstream cin("aaa.txt");
string str;
while(cin>>str)
{
ms.insert(str);
}
multiset<string,cmp>::iterator iter;
for(iter=ms.begin();iter!=ms.end();iter++)
{
cout<<*iter<<endl;
}
return 0;
}
Code:
//首先按长度排序,
//长度一样,按'1'的个数排序,
//‘1’的个数一样时,就按ASCII排序
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <fstream>
//if there is not included fstream libary ,we will get the bottom error:
///main.cpp|26|error: variable `std::ifstream cin' has initializer but incomplete type|
//||=== 完成的Build: 1 个错误, 0 个警告 ===|
using namespace std;
struct cmp//比较算法:从'1'的个数 按小到大排
{
//重载()操作
bool operator () (const string &s1,const string &s2)//返回1 ,0 ,-1
{
if(s1.length()!=s2.length())
return s1.length()<s2.length(); //返回比较长度的逻辑值
int c1 = count(s1.begin(),s1.end(),'1');
int c2 = count(s2.begin(),s2.end(),'1');
return (c1!=c2?c1<c2:s1<s2); //c1等于c2时返回c1<c2的逻辑值,否者返回ASCII值
}
};
int main(int argc,char *argv[])
{
multiset<string,cmp>ms;//Why choose multiset data struct in this place?
ifstream cin("aaa.txt");
string str;
while(cin>>str)
{
ms.insert(str);
}
multiset<string,cmp>::iterator iter;
for(iter=ms.begin();iter!=ms.end();iter++)
{
cout<<*iter<<endl;
}
return 0;
}
相关文章推荐
- JAVA默认排序算法问题 java.lang.IllegalArgumentException
- 算法整理(四):浅析高速排序的优化问题
- [置顶] 算法笔记 //05_有重复元素的排列问题(针对字母排序)
- 计算名次与按名次排序问题的算法优化
- 数组排序问题的两种方法:插入排序算法和递归(分治)算法
- 算法 -TreeSet类的排序问题
- 关于利用STL中的sort算法对向量等容器进行排序的问题
- 算法基础:正整数指定规则排序问题(Golang实现)
- 算法学习笔记——排序问题
- 算法: 找子集合并按权值和排序 (货郎问题辅助算法)
- 笔试算法题(33):烙饼排序问题 & N!阶乘十进制末尾0的个数二进制最低1的位置
- 算法--三色排序(经典的荷兰国旗问题)
- JAVA默认排序算法问题
- JAVA默认排序算法问题
- NSGA-Ⅱ算法做非支配排序后,解丢失问题。
- JAVA默认排序算法问题
- 算法基础:正整数指定规则排序问题(Golang实现)
- 【算法导论实验2】快速排序与第i小数问题
- 开开心心学算法--快速排序之会场安排问题
- 算法基础:数组指定规则排序问题(Golang实现)