从M个不同的整数中,选择N个出来排列

来源:互联网 发布:synthesia mac 编辑:程序博客网 时间:2024/05/17 03:52

#include <IOSTREAM>using namespace std;#define N 3#define M 5//从M个不同元素中选取N个出来排列int x[N];int element[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9};bool map[M]; //map[i] 为true表示element[i]可用,反之不可用int count = 0;void backtrack(int t){if (t>=N){for(int i=0; i<N; ++i)cout << x[i] << " ";cout << endl;count++;}else{for (int i=0; i<M; ++i){if (map[i]){x[t] = element[i];map[i] = false;backtrack(t+1);map[true];}}}}int main(){for (int i=0 ; i<M; ++i)map[i] = true;backtrack(0);cout << "总的结果个数为:" << count << endl;return 0;}

0 0
原创粉丝点击