设有N个整数的集合{1,2,3***},从中任意选r个数进行排列,试举出所有排列
#include <iostream>
int n,r;
int huo[10000];
bool flag[10000];
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
void dfs(int depth){
if(depth>r){
for(int i=1;i<=r;i++)
{
cout<<huo[i]<<" ";
}
cout<<endl;
}
else{
for(int i=1;i<=n;i++){
if( flag[i]==true)
{
huo[depth] =i;
flag[i]=false;
dfs(depth+1);
huo[depth]=0;
flag[i]=true;
}
}
}
}
int main(int argc, char** argv) {
for(int i=1;i<=10000;i++)
flag[i]=true;
cin>>n>>r;
dfs(1);
return 0;
}