前一个日志解决了1-N个数字的排列。假如是N个不同元素的排列,就要用到2个数组,不能简单再用变量i来模拟了。
但是,解决问题的思路是一样的,还是在每个位置上遍历所有可能,判断能否放置该元素,然后向后移动,直到结尾。
//判断B[pos]的元素在A[]中是否已经出现过
int can_place_1(int *A, int *B, int j, int pos)
{
int res=1;
for(int i=0; i<pos; i++)
{
if(A[i]==B[j])
{
//once found j has already existed, then break and return false
res=0;
break;
}
}
return(res);
}
//递归遍历
void hjd_permutation_1(FILE *fp, int *A, int *B, int N, int pos)<