排列递归重复元素?

时间:2016-10-21 21:17:59

标签: c arrays permutation

所以我得到两个参数,一个是N [数组有多大]和nr_vals [这是范围if nr_vals == 2, then range would be ( 0~1)]。我在交换函数EXC_BAD_ACCESS中收到错误。并且排列不正确。任何想法?

我的交换功能;

void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

主要乐趣;

void perm_rec_1(int N, int nr_vals){

    int array[N];
    int tempAr = 0;
    for (int arrayFiller = 0; arrayFiller <= N; arrayFiller++)
    {
        if (arrayFiller == nr_vals){
            tempAr = 0;
        }
        array[arrayFiller] = tempAr;
        tempAr++;
    }
    prem_rec_help(N, nr_vals, array);
}

次要;

void prem_rec_help(int N, int nr_vals, int array[])
{
    int tempArray[N];
    copy_array(array, tempArray, N);
    show(tempArray, N);
    int M = 0;
    int solid = N;
    last_helper(tempArray, array, M, N, solid, nr_vals);
}

递归函数;

void last_helper(int array[],int temp[], int M, int N, int soild, int nr_vals ){

    if (M != N)
    {
        last_helper(array,temp, M+1, N, soild, nr_vals);
    }
    for ( int i = 0; i < nr_vals; i++)
    {
        temp[M] = i;
        show(temp, soild);
    }
    M--;
    if(M == 0)
    {
        for( int swaper = 0; swaper <= soild; swaper++)
        {
            swap(array[swaper], array[swaper+1]);
            copy_array(array, temp, soild);
            if(swaper == soild)
            {
                return;
            }else{
                last_helper(array,temp, M, N, soild, nr_vals);
            }
        }
    }
}

0 个答案:

没有答案