我有一个包含一个数组可以说[25,15,8,20] 我想找到号码的所有可能的安排,这是可能的。
I have an array which contains lets say [25,15,8,20] I want to find all possible arrangements of numbers that is possible.
期望的输出:
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; }
以上程序未给予所有可能的输出。如何获得内部交换和获取组合
The above program is not giving all possible outputs. How do I get the internal swap and get the combinations
推荐答案有几个方面找到的所有排列和排列的数目。图中所示为下面的评论,来计算排列数 K 的总规模群体对 N 元素,你会发现在阶乘 N 的阶乘为数除以氏/ code>尺寸组,可以弥补 ñ元素。为了找到所有排列的所有4个元素的四个元素的数组的情况下,有 24 可能的排列。
There are several aspect to finding all permutations and the number of permutations. Shown with comments below, to calculate the number of permutations for k size groups in a total on n elements, you find the factorial on n divided by the factorial for number of k size groups that can make up n elements. For the case of finding all permutations for all 4 elements in a four elements array, there are 24 possible permutations.
可用的排列,然后递归发现。查看以下内容,让我知道如果你有任何问题:
The permutations available are then found recursively. Look over the following and let me know if you have any questions:
#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注意:作为字符串,这种方式工作得很好,但确实的没有的结果,在可能的排列的词汇分类
Note: for strings, this approach works fine, but does not result in a lexical sorting of the possible permutations.
更多推荐
查找号码的所有可能安排在一个数组
发布评论