#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[i] = true;
}
}
}
}
int main()
{
for (int i=0 ; i<M; ++i)
map[i] = true;
backtrack(0);
cout << "总的结果个数为:" << count << endl;
return 0;
}
从M个不同的整数中,选择N个出来排列
于 2014-09-09 15:29:11 首次发布