1. To find the total number of permutations combined results
Combination: Using recursive algorithm, according to the second line of formula below.
int Sumzuhe ( int N, int K) { If (K = = 0 ) return 1 ; if (N == K) return 1 ; return Sumzuhe (N-1 , K-1 ) + Sumzuhe (N-1 , K);}
permutation: Recursive. Thought from: 73863086.
int sumpailie (int N,int K) { if (K = =1) return N; return 1 1) *N;}
2. Display arrangement, combined results.
Arrangement: First from the (N) to take a number, and then in the remaining time to take a number, each take a number of the mark is taken, so as not to take it again. After taking the number of K, the number of K output, showing the results (so need to have an array in advance to hold the results). Then take the search for another number of K, in order to continue to look for other (K-1), (K-2),,,,, number. Take a number to set the mark to not been taken.
voidPailie (intA[],intNintKintLevel )//(K==n) is the full arrangement {if(level>=K) { for(intj =0; J < level; J + +) printf ("%d", Result[j]); printf ("\ n"); return; } for(inti =0; i < N; i++) { if(Flag[i] = =false)//The bit has not been taken {Flag[i]=true; Result[level++] =a[i];//Remove the modified marker bit Pailie (A, N, K, level);//choose one of the unused insidelevel--;//re-fetch another bit flag[i]=false; } }}
combinations: Combinations and permutations are different: not in order. We can assume that we have always been back to the previous selection, then the first number as the beginning, then we can not start again. For example: A,b,c,d. When I first select the first number is a, after the number of a is selected, the next selection of the first number must not be a. Therefore, a variable is required to control the first number selected (the program below is index). Then pick the next number in the number after the first number (for example, select a). Selecting the next number is similar to the above arrangement.
voidZuhe (intA[],intNintKintIndexintDeep ) { if(Deep >=K) { for(inti =0; i < K; i++) {printf ("%d", Result[i]); } printf ("\ n"); return; } for(inti = index; I <N; i++) {Result[deep]=A[i]; deep++; Zuhe (A, N, K, index+1, deep); deep--; Index++; }}
Complete program:
#include "stdio.h" #define MAX 10#define length 10typedef enum bool{True,false}bool;char flag[10]; int result[10];int su Mzuhe (int N, int K) {if (k = = 0) return 1;if (N = = k) return 1;return Sumzuhe (N-1, K-1) + Sumzuhe (N-1, k);} void Pailie (int a[],int n,int k,int level) {if (level>=k) {to (int j = 0; J < level; J + +) printf ("%d", RESULT[J]);p rintf ("\ n"); return;} for (int i = 0; i < N; i++) {if (flag[i] = = False) {Flag[i] = true;result[level++] = a[i];p Ailie (A, N, K, level);//Is not used Over the inside and select a level--;flag[i] = false;}} void Zuhe (int a[], int N, int k,int index,int deep) {if (deep >= K) {for (int i = 0; i < K; i++) {printf ("%d", result [i]);} printf ("\ n"); return;} for (int i = index; I <N; i++) {Result[deep] = A[i];d Eep++;zuhe (A, N, K, index + 1, deep);d eep--;index++;}} int Sumpailie (int n,int K) {if (k ==1) return N;return Sumpailie (N-1, K-1) *n;} void Main () {int a[5] = {1,2,3,4,5};memset (flag, False, sizeof);p rintf ("Number of permutations (5,3): \ n");p rintf ("%d", Sumpailie(5, 3)); printf ("\ n");p rintf ("permutation result (5,3): \ n");p Ailie (A, 5, 3, 0);p rintf ("full array result: \ n");p Ailie (A, 5, 5, 0);p rintf ("Combined results (5,3): \ n") ;p rintf ("%d", Sumzuhe (5, 3));p rintf ("\ n");p rintf ("combination result (5,3): \ n"), Zuhe (A, 5, 3, 0, 0);p rintf ("\ n");
C Language Realization permutation combination