您的位置:首页 > 其它

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

2014-09-09 15:29 267 查看
#include <IOSTREAM>
using namespace std;
#define N 3
#define M 5
//从M个不同元素中选取N个出来排列
int x
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐