发现在C中环单转矢量矢量、中环、现在

2023-09-11 23:12:06 作者:佛系人生

我的输入作为数组 A = [2,3,4,1]

输出只是从一个元素可以由单个换位(两个相邻元素单翻转)操作来完成所有可能的排列。所以输出是:

  [3,2,4,1],[2,4,3,1],[2,3,1,4],[1,3,4,2 ]
 

的通知transpositioning是允许的。因此, [2,3,4,1] ==> [1,3,4,2] 允许和一个有效的输出。

如何做到在C?

修改 在蟒蛇,它会做如下:

 高清移调(ALIST):
    leveloutput = []
    N = LEN(ALIST)
    因为我在范围内(N):
        X = ALIST [:]
        X [I]中,x [第(i + 1)%N] = X [第(i + 1)%N],X [i]于
        leveloutput.append(X)
    返回leveloutput
 
中环十年 财富号评论 cfhpl

解决方案

该解决方案使用动态内存分配,这样一来,你可以做到这一点的尺寸尺寸的数组。

 为int * swapvalues​​(const int的*常量数组,为size_t大小,廉政左,右诠释)
{
    INT *输出;
    INT sotred;

    输出=的malloc(尺寸*的sizeof(INT));
    如果(输出== NULL)/ *检查成功* /
        返回NULL;
    / *原始值复制到新阵列* /
    的memcpy(输出数组的大小* sizeof的(INT));
    / *交换请求的值* /
    sotred =输出[左]
    输出[左] =输出[右]
    输出[右] = sotred;

    返回输出;
}

INT **转(const int的*常量数组,为size_t大小)
{
    INT **输出;
    INT I;
    诠释J;

    / *生成数组的一个交换副本。 * /
    输出=的malloc(大小* sizeof运算(INT *));
    如果(输出== NULL)/ *检查成功* /
        返回NULL;
    J = 0;
    对于(i = 0; I<尺寸 -  1 ++ I)
    {
        / *分配空间`size`整数* /
        输出[I] = swapvalues​​(数组的大小,J,1 + J);
        如果(输出[I] == NULL)
            转到清理;
        / *在下次迭代交换下面两个值* /
        J + = 1;
    }
    / *现在做同样的第一和最后一个元素* /
    输出[I] = swapvalues​​(数组,大小0,大小 -  1);
    如果(输出[I] == NULL)
        转到清理;
    返回输出;

清理:/ *一些malloc调用返回NULL,清理和退出。 * /
    如果(输出== NULL)
        返回NULL;
    为(J =; J> = 0; j--)
        免费(输出[J]);
    免费(输出);

    返回NULL;
}

诠释的main()
{
    int数组[4] = {2,3,4,1};
    INT I;
    INT **排列=转(阵列的sizeof(阵列)/的sizeof(数组[0]));
    如果(排列!= NULL)
    {
        对于(I = 0; I&4; ++ⅰ)
        {
            诠释J;

            fprintf中(错误,[);
            为(J = 0; J&4; ++ j)条
            {
                fprintf中(错误,%D,排列[I] [J]);
            }
            fprintf中(错误,]);
            免费(排列[I]);
        }
        fprintf中(错误,\ N);
    }
    免费(置换);

    返回0;
}
 

虽然有人认为 GOTO 是邪恶的,这是一个非常不错的使用它,不要用它来控制你的程序的流程(例如创建一个循环),即混乱。但对于出口点的函数,有返回前做几件事情,它认为这实际上是一个很好的使用,这是我的意见,对我来说这使得code更容易理解,我可能是错的。

I have the input as array A = [ 2,3,4,1]

The output is simply all possible permutation from elements in A which can be done by single transposition (single flip of two neighbouring elements) operation. So the output is :

[3,2,4,1],[ 2,4,3,1],[2,3,1,4],[1,3,4,2]

Circular transpositioning is allowed. Hence [2,3,4,1] ==> [1,3,4,2] is allowed and a valid output.

How to do it in C?

EDIT In python, it would be done as follows:

def Transpose(alist):
    leveloutput = []
    n = len(alist)
    for i in range(n):
        x=alist[:]
        x[i],x[(i+1)%n] = x[(i+1)%n],x[i]
        leveloutput.append(x)
    return leveloutput

解决方案

This solution uses dynamic memory allocation, this way you can do it for an array of size size.

int *swapvalues(const int *const array, size_t size, int left, int right)
{
    int *output;
    int  sotred;

    output = malloc(size * sizeof(int));
    if (output == NULL) /* check for success */
        return NULL;
    /* copy the original values into the new array */
    memcpy(output, array, size * sizeof(int));
    /* swap the requested values */
    sotred        = output[left];
    output[left]  = output[right];
    output[right] = sotred;

    return output;
}

int **transpose(const int *const array, size_t size)
{
    int **output;
    int   i;
    int   j;

    /* generate a swapped copy of the array. */
    output = malloc(size * sizeof(int *));
    if (output == NULL) /* check success */
        return NULL;
    j = 0;
    for (i = 0 ; i < size - 1 ; ++i)
    {
        /* allocate space for `size` ints */
        output[i] = swapvalues(array, size, j, 1 + j);
        if (output[i] == NULL)
            goto cleanup;
        /* in the next iteration swap the next two values */
        j += 1;
    }
    /* do the same to the first and last element now */
    output[i] = swapvalues(array, size, 0, size - 1);
    if (output[i] == NULL)
        goto cleanup;
    return output;

cleanup: /* some malloc call returned NULL, clean up and exit. */
    if (output == NULL)
        return NULL;
    for (j = i ; j >= 0 ; j--)
        free(output[j]);
    free(output);

    return NULL;
}

int main()
{
    int array[4] = {2, 3, 4, 1};
    int i;
    int **permutations = transpose(array, sizeof(array) / sizeof(array[0]));
    if (permutations != NULL)
    {
        for (i = 0 ; i < 4 ; ++i)
        {
            int j;

            fprintf(stderr, "[ ");
            for (j = 0 ; j < 4 ; ++j)
            {
                fprintf(stderr, "%d ", permutations[i][j]);
            }
            fprintf(stderr, "] ");
            free(permutations[i]);
        }
        fprintf(stderr, "\n");
    }
    free(permutations);

    return 0;
}

Although some people think goto is evil, this is a very nice use for it, don't use it to control the flow of your program (for instance to create a loop), that is confusing. But for the exit point of a function that has to do several things before returning, it think it's actually a nice use, it's my opinion, for me it makes the code easier to understand, I might be wrong.