题目;
将01串首先按长度排序,长度相同时,按1的个数多少进行排序,1的个数相同时再按ASCII码值排序。
将01串首先按长度排序,长度相同时,按1的个数多少进行排序,1的个数相同时再按ASCII码值排序。
#include <set>
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
class Comp
{
public:
bool operator ()(const string &sa, const string &sb)
{
if (sa.length() != sb.length()) return sa.length() < sb.length();
int ca = count(sa.begin(), sa.end(), '1');
int cb = count(sb.begin(), sb.end(), '1');
return (ca != cb ? ca < cb : sa < sb);
}
};
int main()
{
multiset<string, Comp> ms;
string str;
while (cin >> str)
ms.insert(str);
copy(ms.begin(), ms.end(),
ostream_iterator<string>(cout, "\n"));
}