#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool Mycomp(const string &s1, const string &s2)
{
int c1 = count(s1.begin(), s1.end(), “1”);
int c2 = count(s2.begin(), s2.end(), “1”);
return c1 != c2 ? c1 < c2 : c1 < c2;
}
int main()
{
vector<string> result;
string str;
while (cin >> str)
{
result.push_back(str);
}
stable_sort(str.begin(), str.end(),Mycomp);
for (vector<string>::iterator it = result.begin(); it != result.end();it++)
{
cout << *it << endl;
}
return 0;
}
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool Mycomp(const string &s1, const string &s2)
{
int c1 = count(s1.begin(), s1.end(), “1”);
int c2 = count(s2.begin(), s2.end(), “1”);
return c1 != c2 ? c1 < c2 : c1 < c2;
}
int main()
{
vector<string> result;
string str;
while (cin >> str)
{
result.push_back(str);
}
stable_sort(str.begin(), str.end(),Mycomp);
for (vector<string>::iterator it = result.begin(); it != result.end();it++)
{
cout << *it << endl;
}
return 0;
}
解决方案
7
stable_sort(result.begin(), result.end(),Mycomp);
6
stable_sort()
这个函数调用错了, 你是想 stable_sort(result.begin(), result.end(),Mycomp);吧?
这个函数调用错了, 你是想 stable_sort(result.begin(), result.end(),Mycomp);吧?
14
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; bool Mycomp(const string &s1, const string &s2) { int c1 = count(s1.begin(), s1.end(), "1"); int c2 = count(s2.begin(), s2.end(), "1"); return c1 != c2 ? c1 < c2 : c1 < c2; } int main() { vector<string> result; string str; while (cin >> str) { result.push_back(str); } stable_sort(result.begin(), result.end(),Mycomp); for (vector<string>::iterator it = result.begin(); it != result.end();it++) { cout << *it << endl; } return 0; }