排列组合的实现

排列:

隐藏行号 复制代码 Demo
  1. #include
         
          
    
         
         
  2. #include
         
         
    
         
         
  3. #define ELEMENT std::string
    
  4. namespace dskit
    
  5. {
    
  6. typedef void (*PrintFunc)(const ELEMENT* elements, const size_t size);
    
  7. class perm
    
  8. {
    
  9. public:
    
  10.     perm(size_t size, PrintFunc func_print):m_size(size), m_func_print(func_print) {}
    
  11.     void operator()(ELEMENT* elements, size_t start, size_t end);
    
  12. private:
    
  13.     void swap(ELEMENT& lhs, ELEMENT& rhs)
    
  14.     {
    
  15.         if(lhs == rhs)
    
  16.             return;
    
  17.         ELEMENT temp = lhs;
    
  18.         lhs = rhs;
    
  19.         rhs = temp;
    
  20.     }
    
  21. private:
    
  22.     size_t m_size;
    
  23.     PrintFunc m_func_print;
    
  24. };
    
  25. void perm::operator()(ELEMENT* elements, size_t start, size_t end)
    
  26. {
    
  27.     if(start == end)
    
  28.     {
    
  29.         m_func_print(elements, m_size);        
    
  30.     }
    
  31.     else
    
  32.     {
    
  33.         for(size_t i = start; i != end; ++i)
    
  34.         {
    
  35.             swap(elements[i], elements[start]);
    
  36.             operator()(elements, start + 1, end);
    
  37.             swap(elements[i], elements[start]);
    
  38.         }    
    
  39.     }
    
  40. }
    
  41. }
    
  42. void print(const ELEMENT* elements, const size_t size)
    
  43. {
    
  44.     for(size_t i = 0; i != size; ++i)
    
  45.     {
    
  46.         std::cout << elements[i] << '/t';    
    
  47.     }
    
  48.     std::cout << std::endl;
    
  49. }
    
  50. int main(int argc, char* argv[])
    
  51. {
    
  52.     ELEMENT array[] = {"d", "da", "af", "5", "8"};
    
  53.     dskit::perm(sizeof(array) / sizeof(ELEMENT), print)(array, 0, sizeof(array) / sizeof(ELEMENT));    
    
  54. }
    
<script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i  

组合:

隐藏行号 复制代码 Demo
  1. #ifndef __COMBINATION_H__
    
  2. #define __COMBINATION_H__
    
  3. #include
          
          
    
          
          
  4. typedef int ELEMENTTYPE;
    
  5. #define MAXELEMENT 100
    
  6. void combination(std::ostream& out, const ELEMENTTYPE* src, size_t n_set);
    
  7. void combination_sorted(std::ostream& out, const ELEMENTTYPE* src, size_t n_set, size_t n_subset, size_t start_pos = 0);
    
  8. void combination_sorted(std::ostream& out, const ELEMENTTYPE* src, size_t n_set);
    
  9. #endif
    
<script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i  

隐藏行号 复制代码 Demo
  1. #include
           
           
    
           
           
  2. #include
           
           
    
           
           
  3. #include"combination.h"
    
  4. ELEMENTTYPE res[MAXELEMENT]; 
    
  5. void combination(std::ostream& out, const ELEMENTTYPE* src, size_t n_set)
    
  6. {
    
  7.     if(n_set > 63)
    
  8.         return;
    
  9.     for(size_t i = 0; i < (1 << n_set); ++i)
    
  10.     {
    
  11.         std::bitset
           
           
            
             bs(i);
    
           
           
  12.         for(size_t j = 0; j < n_set; ++j)
    
  13.             if(bs[j]) ;
    
  14.                 out << src[j] << "/t";
    
  15.         out << std::endl;
    
  16.     }
    
  17. }
    
  18. void combination_sorted(std::ostream& out, const ELEMENTTYPE* src, size_t n_set, size_t n_subset, size_t start_pos)
    
  19. {
    
  20.     static size_t dep = 0;
    
  21.     if(0 == n_subset)
    
  22.     {
    
  23.         for(size_t index = 0; index < dep; ++index)
    
  24.             out << res[index] << "/t";
    
  25.             out << std::endl;
    
  26.     }
    
  27.     else
    
  28.     {
    
  29.         for(size_t i = start_pos; i <= n_set - n_subset; ++i)
    
  30.         {
    
  31.             res[dep] = src[i];
    
  32.             ++dep;
    
  33.             combination_sorted(out, src, n_set, n_subset - 1, i + 1);    
    
  34.             --dep;
    
  35.         }    
    
  36.     }    
    
  37. }
    
  38. void combination_sorted(std::ostream& out, const ELEMENTTYPE* src, size_t n_set)
    
  39. {
    
  40.     for(size_t i = 0; i != n_set + 1; ++i)
    
  41.         combination_sorted(out, src, n_set, i, 0);
    
  42. }
    
<script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值