这段时间没咋编程序
就写个c++排列组合函数的使用吧
以后使用得着的
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/assign.hpp>
#include <boost/function.hpp>
using namespace std;
using namespace boost;
using namespace boost::assign;
inline void print_(int t){cout<<t<<" ";}
inline void print(vector<int>& vec)
{
for_each(vec.begin(),vec.end(),print_);
cout<<endl;
}
//! 全排列测试
void test1()
{
vector<int> vec;
vec += 1,2,3,4,5,6,7,8;
sort(vec.begin(),vec.end());
int i = 0;
do
{
print(vec);
i++;
}
while(next_permutation(vec.begin(),vec.end()));
std::cout<<i<<std::endl;
}
//! 组合测试
size_t test2(int n,int m,boost::function<void(std::vector<int>& vec)> fn)
{
vector<int> p,set;
p.insert(p.end(),m,1);
p.insert(p.end(),n-m,0);
for(int i = 0;i != p.size();++i)
set.push_back(i+1);
vector<int> vec;
size_t cnt = 0;
do{
for(int i = 0;i != p.size();++i)
if(p[i])
vec.push_back(set[i]);
fn(vec);
cnt ++;
vec.clear();
}while(prev_permutation( p.begin(), p.end()));
return cnt;
}
int main()
{
test1();
std::cout<<test2(20,3,print)<<std::endl;
return 0;
}
....................................................................................................................