permutations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <stdio.h> int permutations (int* Array, const int Start_Point, const int End_Point); void swap (int* current, int* next); int main (int argc, char** argv) { int Array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int count = permutations (Array, 0, 3 - 1); printf ("\n\n%d\n", count); return 0; } int permutations (int* Array, const int Start_Point, const int End_Point) { static int count = 0; if (Start_Point == End_Point) { int index = 0; for (index = 0; index <= End_Point; index++) { printf ("%d ", *(Array + index)); } printf ("\n"); count++; } else { int index = 0; for (index = Start_Point; index <= End_Point; index++) { swap (&(*(Array + Start_Point)), &(*(Array + index))); count = permutations (Array, Start_Point + 1, End_Point); swap (&(*(Array + Start_Point)), &(*(Array + index))); } } return count; } void swap (int* current, int* next) { int temporary = *current; *current = *next; *next = temporary; return; } | cs |
0 개의 댓글:
댓글 쓰기