C中的迭代置入功能

发布于 2025-01-17 13:33:26 字数 602 浏览 2 评论 0原文

我正在尝试编写一个迭代函数,该功能计算输入中给出的数字数组的所有排列。 这是我到目前为止写的代码。

void permute(int *a, int size){
  int j=0, i, h=0, m;
  bool flag=true;
  int f = factorial(size);
  int *arr, *res;
  int counter=0;

  arr = malloc(f*sizeof(int));
  

  for(i=0; i<f; i++)
    arr[i] = 0;

  while (j < f) {
    if(arr[j]<j)
    {
      if(j%2 == 0)
      {
        swap(a[0],a[j]);
      } else {
        swap(a[arr[j]], a[j]);
      }

      arr[j]++;

      j=0;
    } else{
      arr[j] = 0;
      j++;
    }
    printf("%d\n",a[j] );
    }
}

该代码不能很好地计算所有排列,并且会陷入较长的循环。有人可以帮我吗?感谢大家。

I'm trying to write an iterative function that computes all the permutations of an array of numbers given in input.
Here is the code I've written so far.

void permute(int *a, int size){
  int j=0, i, h=0, m;
  bool flag=true;
  int f = factorial(size);
  int *arr, *res;
  int counter=0;

  arr = malloc(f*sizeof(int));
  

  for(i=0; i<f; i++)
    arr[i] = 0;

  while (j < f) {
    if(arr[j]<j)
    {
      if(j%2 == 0)
      {
        swap(a[0],a[j]);
      } else {
        swap(a[arr[j]], a[j]);
      }

      arr[j]++;

      j=0;
    } else{
      arr[j] = 0;
      j++;
    }
    printf("%d\n",a[j] );
    }
}

The code doesn't compute well all the permutations and goes into a long loop. Can someone help me, please? Thanks to everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给妤﹃绝世温柔 2025-01-24 13:33:26

您的代码接近,但包括一些问题。例如,while循环
while(j&lt; f)j分配给来自数组a的值。
相反,您可以尝试:

#include <stdio.h>
#include <stdlib.h>

int factorial(int x)
{
    int i;
    int y = 1;

    for (i = 1; i <= x; i++) {
        y *= i;
    }
    return y;
}

void swap(int *x, int *y)
{
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void permute(int *a, int size)
{
    int i, j = 0;
    int f = factorial(size);
    int *arr;

    arr = calloc(f, sizeof(int));       // the members are initialized to 0

    // print the original array
    for (i = 0; i < size; i++) {
        printf("%d%s", a[i], i == size - 1 ? "\n" : " ");
    }

    while (j < size) {
        if (arr[j] < j) {
            if (j % 2 == 0) {
                swap(a + 0, a + j);
            } else {
                swap(a + arr[j], a + j);
            }

            // print the rearranged array
            for (i = 0; i < size; i++) {
                printf("%d%s", a[i], i == size - 1 ? "\n" : " ");
            }

            arr[j]++;
            j = 0;
        } else {
            arr[j] = 0;
            j++;
        }
    }
    free(arr);
}

int main()
{
    int a[] = {1, 2, 3};                // example
    permute(a, sizeof a / sizeof a[0]); // the 2nd argument is the array length

    return 0;
}

示例的输出:

1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1

Your code is close but includes some problems. For instance, the while loop
while (j < f) will assign j to a value out of bound of the array a.
Instead would you please try:

#include <stdio.h>
#include <stdlib.h>

int factorial(int x)
{
    int i;
    int y = 1;

    for (i = 1; i <= x; i++) {
        y *= i;
    }
    return y;
}

void swap(int *x, int *y)
{
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void permute(int *a, int size)
{
    int i, j = 0;
    int f = factorial(size);
    int *arr;

    arr = calloc(f, sizeof(int));       // the members are initialized to 0

    // print the original array
    for (i = 0; i < size; i++) {
        printf("%d%s", a[i], i == size - 1 ? "\n" : " ");
    }

    while (j < size) {
        if (arr[j] < j) {
            if (j % 2 == 0) {
                swap(a + 0, a + j);
            } else {
                swap(a + arr[j], a + j);
            }

            // print the rearranged array
            for (i = 0; i < size; i++) {
                printf("%d%s", a[i], i == size - 1 ? "\n" : " ");
            }

            arr[j]++;
            j = 0;
        } else {
            arr[j] = 0;
            j++;
        }
    }
    free(arr);
}

int main()
{
    int a[] = {1, 2, 3};                // example
    permute(a, sizeof a / sizeof a[0]); // the 2nd argument is the array length

    return 0;
}

Output of the example:

1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文