#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector < vector < double >> combination( const size_t& choose, const size_t& from) {
vector<string> res;
string wk = string(choose, '1') + string(from - choose, '0');
res.push_back(wk);
size_t found = string::npos;
while ((found = wk.find("10")) != string::npos) {
wk[found] ^= wk[found + 1];
wk[found + 1] ^= wk[found];
wk[found] ^= wk[found + 1];
std::sort(wk.begin(), wk.begin() + found, [](const char &a, const char &b) {return a > b; });
res.push_back(wk);
}
vector < vector < double >> Temp;
for (size_t i = 0; i != res.size(); ++i) {
vector<double> t;
for (size_t j = 0; j != from; ++j) {
if (res[i][j] == '1') {
t.push_back(j + 1);
}
}
Temp.push_back(t);
}
return Temp;
}
int main() {
vector<string> res;
const size_t choose = 5, from = 10;
auto Temp =combination(choose, from);
for (size_t i = 0; i != Temp.size(); ++i) {
cout << Temp[i][0] << " " << Temp[i][1] << " " << Temp[i][2] << " "<< Temp[i][3]<<" "<< Temp[i][4] << endl;
}
cout << Temp.size() << endl;
return 0;
}
c++组合算法
最新推荐文章于 2024-06-26 10:50:11 发布