713A - 自编的组合程序

 想了两天终于写出了一个组合程序,用以输出 123的组合数

 

#include <stdlib.h>

#include <stdio.h>

 

#define N 3

 

int a[N] = {1, 2, 3};

int *pt[N];

 

void permutation(int **, int);

 

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

{

int loop = N;

 

while (loop--)

pt[loop] = &a[loop];

permutation(pt, 0);

 

return EXIT_SUCCESS;

}

 

void permutation(int **ipt, int pos)

{

int loop = 0, shift = 0, i = 0;

int *tmp, *pt_cpy[N];

 

if (pos == N-1) {

while (i++ < N)

printf("%d ", **ipt++);

printf("\n");

 

return;

}

 

while (shift < N-pos) {

while (loop < N) {

pt_cpy[loop] = *ipt++;

loop++;

}

 

tmp = pt_cpy[pos];

pt_cpy[pos] = pt_cpy[shift + pos];

pt_cpy[shift + pos] = tmp;

shift++;

 

permutation(pt_cpy, pos + 1);

}

}

 

 

本文出自 “莫在浮沙筑高台” 博客,谢绝转载!

你可能感兴趣的:(程序,include,123,的)