对一个字符数组进行全排列

#include <stdio.h>
#include <stdlib.h>
void swapArrayElements(char a[], int lhs, int rhs)
{
     char temp;
     temp = a[lhs];
     a[lhs] = a[rhs];
     a[rhs] = temp;
}
void perm(char a[], int start, int end)
{
     int i, j;
     if (start == end){
               //输出排列结果
               for (j = 0; j <= end; j++)
                   putchar(a[j]);
               putchar('/n');
               return;
     } else{
           
            for (i = start; i <= end; i++){
                //将数组片段的各元素与首元素交换
                swapArrayElements(a, start, i);
               
                //对交换后的,去掉首元素的数组片段进行全排列
                perm(a, start+1, end);
               
                //交换回来
                swapArrayElements(a, start, i);
            }
     }
}
main()
{
      char a[] = {'A', 'B', 'C', 'D'};
      perm(a, 0, 3);
      system("pause");
}
发布了434 篇原创文章 · 获赞 93 · 访问量 154万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章