Python';C语言中的s itertools.product

Python';C语言中的s itertools.product,python,c,cartesian-product,Python,C,Cartesian Product,Python中有一个函数以这种方式运行: itertools.product("abc", repeat = 2) 返回以下内容: ("a", "a") ("a", "b") ("a", "c") ("b", "a") ("b", "b") ("b", "c") ("c", "a") ("c", "b") ("c", "c") 更改repeat变量将更改元组中返回的项目数。 如何用C编写它来返回字符数组?(字符串数组) 更新:我现在有了我写的这个函数: void cartesian(char

Python中有一个函数以这种方式运行:

itertools.product("abc", repeat = 2)
返回以下内容:

("a", "a")
("a", "b")
("a", "c")
("b", "a")
("b", "b")
("b", "c")
("c", "a")
("c", "b")
("c", "c")
更改repeat变量将更改元组中返回的项目数。 如何用C编写它来返回字符数组?(字符串数组)

更新:我现在有了我写的这个函数:

void cartesian(char *a, int al, char *b, int bl){
    int i, j;
    char c[al * bl][2];
    for(i = 0; i < al; i++){
        for(j = 0; j < bl; j++){
            c[(i * bl) + j][0] = *(a + i);
            c[(i * bl) + j][1] = *(b + j);
            printf("%c%c\n", *(a + i), *(b + j));
        }
    }
}

int main(){
    char a[] = "abc";
    char b[] = "ab";
    cartesian(a, strlen(a), b, strlen(b));
    return 0;
}
示例阵列:

[
    ['a', 'b', 'c', '\0'],
    ['a', 'b', 'c', '\0'],
    ['a', 'b', 'c', '\0']
]
(计算数组长度时包含空值) 应该产生

[
    ['a', 'a', 'a'],
    ['a', 'a', 'b'],
    ['a', 'a', 'c'],
    ['a', 'b', 'a'],
    ['a', 'b', 'b'],
    ['a', 'b', 'c'],
    ['a', 'c', 'a'],
    ['a', 'c', 'b'],
    ['a', 'c', 'c'],
    ['b', 'a', 'a'],
    ['b', 'a', 'b'],
    ['b', 'a', 'c'],
    ['b', 'b', 'a'],
    ['b', 'b', 'b'],
    ['b', 'b', 'c'],
    ['b', 'c', 'a'],
    ['b', 'c', 'b'],
    ['b', 'c', 'c'],
    ['c', 'a', 'a'],
    ['c', 'a', 'b'],
    ['c', 'a', 'c'],
    ['c', 'b', 'a'],
    ['c', 'b', 'b'],
    ['c', 'b', 'c'],
    ['c', 'c', 'a'],
    ['c', 'c', 'b'],
    ['c', 'c', 'c'],
]

下面的C程序创建所有可能的“abc”排列

#包括
#包括
/*函数在两个指针处交换值*/
无效交换(字符*x,字符*y)
{
焦炭温度;
温度=*x;
*x=*y;
*y=温度;
}
无效排列(字符*a,整数l,整数r)
{
int i;
如果(l==r)
printf(“%s\n”,a);
其他的
{

对于(i=l;i,根据您的规范,这里是笛卡尔积的C实现。请注意,参数是
char**a
,而不是
char*a
,因为它是字符串数组

这是一种基于

void笛卡尔(字符**a,无符号整数l)
{
无符号整数*索引=calloc(l,sizeof(int));
无符号整数改变;
做{
无符号整数finished=0;
无符号整数i;
改变=0;
/*打印当前元组*/
对于(i=0;i
Python版本是用C:编写的,繁重的工作在
product_next
函数中完成。OP想要的是笛卡尔积,而不是置换。我不确定他到底想实现什么,因此我还是发布了代码。这也可以迭代完成,而不是递归完成吗?对于置换算法probab当然不是。但是笛卡尔积当然是。这也是为什么我也提供了一个链接到C++代码的同一个问题。如果你知道C,它应该是很容易跟进。
[
    ['a', 'a', 'a'],
    ['a', 'a', 'b'],
    ['a', 'a', 'c'],
    ['a', 'b', 'a'],
    ['a', 'b', 'b'],
    ['a', 'b', 'c'],
    ['a', 'c', 'a'],
    ['a', 'c', 'b'],
    ['a', 'c', 'c'],
    ['b', 'a', 'a'],
    ['b', 'a', 'b'],
    ['b', 'a', 'c'],
    ['b', 'b', 'a'],
    ['b', 'b', 'b'],
    ['b', 'b', 'c'],
    ['b', 'c', 'a'],
    ['b', 'c', 'b'],
    ['b', 'c', 'c'],
    ['c', 'a', 'a'],
    ['c', 'a', 'b'],
    ['c', 'a', 'c'],
    ['c', 'b', 'a'],
    ['c', 'b', 'b'],
    ['c', 'b', 'c'],
    ['c', 'c', 'a'],
    ['c', 'c', 'b'],
    ['c', 'c', 'c'],
]
#include <stdio.h>
#include <string.h>

/* Function to swap values at two pointers */
void swap(char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void permute(char *a, int l, int r)
{
   int i;
   if (l == r)
     printf("%s\n", a);
   else
   {
       for (i = l; i <= r; i++)
       {
          swap((a+l), (a+i));
          permute(a, l+1, r);
          swap((a+l), (a+i)); //backtrack
       }
   }
}

int main()
{
    char str[] = "abc";
    int n = strlen(str);
    permute(str, 0, n-1);
    return 0;
}
    void cartesian(char **a, unsigned int l)
    {
        unsigned int *indices = calloc(l, sizeof(int));
        unsigned int changed;
        do {
            unsigned int finished = 0;
            unsigned int i;
            changed = 0;
            /* Print the current tuple */
            for (i = 0; i < l; i++) {
                putchar(a[i][indices[i]]);
            }
            putchar('\n');
            /* Loop over the arrays in reverse order */
            for (i = l - 1; !changed && !finished; i--) {
                /* Increment */
                indices[i]++;
                if (a[i][indices[i]]) {
                    /* We moved to the next character */
                    changed = 1;
                }
                else {
                    /* End of string, so roll over */
                    indices[i] = 0;
                }
                finished = i == 0;
            }
        } while (changed);
        free(indices);
    }