ITPub博客

首页 > > > 索引组合算法原型

索引组合算法原型

作者:友哥 时间:2018-08-15 19:45:00 0 删除 编辑
// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort

int make_combination(int myints[], int start , int end, int requirement_number){
    //std::cout<<"start:"<<start<<"end:"<<end<<"requirement_number:"<<requirement_number<<std::endl;
    if (start == end){
        std::cout << myints[start] << ",";
        requirement_number--;
        if (requirement_number >= 1){
            std::cout << "not ok" << std::endl;
            return 1;
        }else {
            std::cout << std::endl;
            return 0;
        }
    }
    if (1 ==requirement_number){
        requirement_number--;
        std::cout << myints[start] << std::endl;
        return 0;
    }
    std::cout<<myints[start];
    requirement_number--;
    return make_combination(myints, start+1, end, requirement_number );
}

int main () {
  int myints[] = {1,2,3};

  std::sort (myints,myints+3);
  int i =0;
  for(; i<3; i++){
    make_combination(myints, i, 2, 1);
  }

//  std::cout << "The 3! possible permutations with 3 elements:\n";
//  do {
//    std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
//  } while ( std::prev_permutation(myints,myints+3) );
//
//  std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';

  return 0;
}

 有bug,比如 1,2,3

只能给出

1,2

2,3

但是看不到

1,3

 

原文作者:;如有侵权,请联系管理员删除。

上一篇: 算法真的复杂
请登录后发表评论 登录
全部评论

注册时间:2020-05-20

  • 博文量
    276
  • 访问量
    1000