C language implementation of permutation/combination algorithm

Source: Internet
Author: User
/*
* FileName: PERMUTATION.C
* Use: Full array algorithm
* Programming Environment: WinXP SP2+CL 8.0
* Date of Completion: 2006.2 Ver 0.01
* Author: 88250
* Contact: e-mail:dl88250@gmail.com qq:845765
*/

#include < stdio.h >

#include < stdlib.h >



int count = 0;



void permutation (char per[], int m, int len)

{

int i;

char tmp;



if (M < len-1) {

Permutation (per, M + 1, len);

for (i = m + 1; i < Len; i + +) {

/* Select the time of the arrangement of "head" * *

TMP = Per[m];

PER[M] = Per[i];

Per[i] = tmp;

Permutation (per, M + 1, len);

/* Restore previous arrangement * *

TMP = Per[m];

PER[M] = Per[i];

Per[i] = tmp;

}

} else {

+ + count; /* To count the permutations * *

/* Adjust the format ^ ^ * *

printf ("%s", per);

}

return;

}

int main (void)

{

int i = time (NULL);

Char per[] = "ABCD";



Permutation (per, 0, strlen (per));

printf ("Total:%d", count);



return 0;

}

/*
* FileName: COMBINATION.C
* Use: Combinatorial algorithm
* Programming Environment: WinXP SP2+CL 8.0
* Date of Completion: 2006.2 Ver 0.01
* Author: 88250
* Contact: e-mail:dl88250@gmail.com qq:845765
*/

#include < stdio.h >



#define MAX_NUM 20



int Comb[max_num];



void combination (int m, int n)

{

int I, J;



for (i = m; i >= N; i--) {

Comb[n] = i; /* Select the current "header" element * *

if (n > 1) {

/* Go to the next smaller combination problem * *

Combination (i-1, n-1);

} else {

/* Full number of combinations required, output * *

for (j = comb[0]; j > 0; J--) {

printf ("%c", Comb[j] + 64);

}

printf ("");

}

}



return;

}



int main (int argc, char * argv[])

{



comb[0] = 2;

Combination (4, comb[0]); /* C (4, 2) */



return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.