+++++++++++++++++++++++++++++++++++
++++++++++++源程序++++++++++++++++++
+++++++++++++++++++++++++++++++++++
#include <stdio.h>
int n = 0; //统计总共有多少组
void swap(int *a, int *b)
{
int m;
m = *a;
*a = *b;
*b = m;
}
void perm(int list[], int k, int m)
{
int i;
if(k > m)
{
for(i = 0; i <= m; i++)
printf("%d ", list[i]);
printf("/n");
n++;
}
else
{
for(i = k; i <= m; i++)
{
swap(&list[k], &list[i]); //换下数字
perm(list, k + 1, m);
swap(&list[k], &list[i]); //在把刚才的还回来
}
}
}
int main()
{
int list[] = {1, 2, 3, 4, 5};
perm(list, 0, 4); //注意长度
printf("total:%d/n", n);
return 0;
}
++++++++++++++++++++++++++++++++++++++++++
+++++++++下面是我修改过的,可以进行string排列++
++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <string.h>
int n = 0;
void swap(char *a, char *b) //改下data type
{
char m;
m = *a;
*a = *b;
*b = m;
}
void perm(char list[], int k, int m)
{
int i;
if(k > m)
{
printf("%s/n", list);
n++;
}
else
{
for(i = k; i<= m; i++)
{
swap(&list[k], &list[i]);
perm(list, k+1, m);
swap(&list[k], &list[i]);
}
}
}//close function
int main(int argc, char **argv)
{
char list[] = "ABC";
perm(list, 0, strlen(list) - 1); //记住,开始是0, 结束为strlen(list)-1
printf("total:%d/n", n);
return 0;
}
+++++++++++++++++++++++++++++++++++++++++
第一个程序是从第一个url里面看的,这个源程序比第二url里面的源程序清晰,
相对比较容易理解,而且在改过程序后,也可以进行string字符交换,所以推
荐第一个,不过根据个人喜好而来。
还有的话,就是,可以把程序改的更复杂点,根据用户需要的字符来排列。
下载地址