#include<iostream.h>
void main(){
int i,a[10]={0},b[10]={1,2,3,4,5,6,7,8,9},g,m=3,n=6,j,s=0;
i=1;
a[i]=1;
while(1){
g=1;
for(j=0;j<i;j++){
if(a[j]==a[i]){
g=0;
break;
}
}
if(g&&i==m){
for(int k=1;k<=m;k++){
cout<<b[a[k]-1]<<" ";
}
cout<<endl;
s++;
}
if(g&&i<m){
i++;
a[i]=1;
continue;
}
while(a[i]==n)
i--;
if(i>0)
a[i]++;
else{
cout<<s<<endl;
break;
}
}
}
回溯法解决排列A(m,n)
最新推荐文章于 2022-01-10 21:36:47 发布