簡體   English   中英

查找數組中數字的所有可能排列

[英]Find all possible arrangements of a n numbers in an array

我有一個包含說[25,15,8,20]的數組,我想找到所有可能的數字排列方式。

預期輸出:

25 15 8 20
25 15 20 8
25 20 15 8
25 20 8 15
25 8 20 15
25 8 15 20
15 25 8 20
15 25 20 8
15 20 25 8
15 20 8 25
15 8 20 25
15 8 25 20
20 25 15 8
20 25 8 15
20 8 25 15
20 8 15 25
20 15 25 8
20 15 8 25
8 15 20 25
8 15 25 20
8 25 15 20
8 25 20 15
8 20 15 25
8 20 25 15


void print(int *num, int n)
{
 int i;
 for ( i = 0 ; i < n ; i++)
    printf("%d ", num[i]);
   printf("\n");
}
int main()
{
 int num[N];
 int *ptr;
 int temp;
 int i, n, j;
 printf("\nHow many number you want to enter: ");
    scanf("%d", &n);
 printf("\nEnter a list of numbers to see all combinations:\n");
 for (i = 0 ; i < n; i++)
    scanf("%d", &num[i]);
 for (j = 1; j <= n; j++) {
    for (i = 0; i < n-1; i++) {
        temp = num[i];
        num[i] = num[i+1];
        num[i+1] = temp;
        print(num, n);
 }
}
 return 0;
}

上面的程序沒有給出所有可能的輸出。 如何獲得內部交換並獲得組合

  • 訣竅是,您不能只置換相鄰的值,請注意,當3固定在index = 1時,永遠不會保留4,因此您必須逐漸將置換擴展到其他值。

     #include <stdio.h> #include <string.h> #include <malloc.h> void print(int *num, int n) { int i; for ( i = 0 ; i < n ; i++) printf("%d ", num[i]); printf("\\n"); } int*permute(int*i,int h) { int temp = *i; *i = *(i+h); *(i+h) = temp; return i+1; } void recursive_permute(int*i,int *j,int n) { if((ji)==n-1) {print(i,n);return;}; int *tmparray=(int*)malloc(n*sizeof(int)); memcpy(tmparray,i,n*sizeof(int)); recursive_permute(tmparray,tmparray+(j-i+1),n); for (int h=1;h<n-(ji);h++) recursive_permute(tmparray,permute(tmparray+(ji),h),n); } int main() { int num[100]; int *ptr; int temp; int i, n, j; printf("\\nHow many number you want to enter: "); scanf("%d", &n); printf("\\nEnter a list of numbers to see all combinations:\\n"); for (i = 0 ; i < n; i++) scanf("%d", &num[i]); printf("my recursive method ---------------------------\\n"); recursive_permute(num,num,n); printf("your method -----------------------------------\\n"); for (j = 1; j <= n; j++) { for (i = 0; i < n-1; i++) { temp = num[i]; num[i] = num[i+1]; num[i+1] = temp; print(num, n); } } return 0; } 

看到它在這里運行

查找所有排列和排列數量有多個方面。 如下面的注釋所示,要計算n元素上總共k大小組的排列數,可以找到n上的階乘除以可以構成n元素的k大小組的數量的階乘。 對於在四個元素數組中查找所有四個元素的所有排列的情況,有24可能的排列。

然后,遞歸找到可用的排列。 查看以下內容,如果您有任何疑問,請告訴我:

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

void swap (int *x, int *y);
unsigned long long nfact (size_t n);
unsigned long long pnk (size_t n, size_t k);
void permute (int *a, size_t i, size_t n);
void prnarray (int *a, size_t sz);

int main (void) {

    int array[] = { 25, 15, 8, 20 };
    size_t sz = sizeof array/sizeof *array;

    /* calculate the number of permutations */
    unsigned long long p = pnk (sz , sz);
    printf ("\n total permutations : %llu\n\n", p);

    /* permute the array of numbers */
    printf (" permutations:\n\n");
    permute (array, 0, sz);
    putchar ('\n');

    return 0;
}

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

/* calculate n factorial */
unsigned long long nfact (size_t n)
{   if (n <= 0) return 1;
    unsigned long long s = n;

    while (--n) s *= n;

    return s;
}

/* calculate possible permutations */
unsigned long long pnk (size_t n, size_t k)
{   size_t d = (k < n ) ? n - k : 1;
    return nfact (n) / nfact (d);
}

/* permute integer array for elements 'i' through 'n' */
void permute (int *a, size_t i, size_t n)
{   size_t j;
    if (i == n)
        prnarray (a, n);
    else
        for (j = i; j < n; j++) {
            swap ((a+i), (a+j));
            permute (a, i+1, n);
            swap ((a+i), (a+j));  // backtrack
        }
}

void prnarray (int *a, size_t sz)
{   size_t i;
    for (i = 0; i < sz; i++) printf (" %2d", a[i]);
    putchar ('\n');
}

使用/輸出

$ ./bin/permute4int

 total permutations : 24

 permutations:

 25 15  8 20
 25 15 20  8
 25  8 15 20
 25  8 20 15
 25 20  8 15
 25 20 15  8
 15 25  8 20
 15 25 20  8
 15  8 25 20
 15  8 20 25
 15 20  8 25
 15 20 25  8
  8 15 25 20
  8 15 20 25
  8 25 15 20
  8 25 20 15
  8 20 25 15
  8 20 15 25
 20 15  8 25
 20 15 25  8
 20  8 15 25
 20  8 25 15
 20 25  8 15
 20 25 15  8

注:字符串,這種方式工作得很好,但不會導致可能的排列的詞匯排序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM