- #include
- #include
- #include <string.h>voidSwap(char*a,char*b)
- {
- chart = *a;
- *a = *b;
- *b = t;
- }
- //在s数组中,[lo,hi)中是否有数字与下标为hi的数字相等
- intswap(char*s,intlo,int hi)
- {
- int i;
- for(i = lo; i < hi; i++)
- if(s[i] == s[hi])
- return 0;
- return 1;
- }
- //k表示当前选取到第几个数,m表示共有多少数.
- voidAllRange(char*s,intk,int m)
- {
- int i;
- if(k == m)
- {
- static intx =1;
- printf(" 第=个排列\t%s\n", x++, s);
- }
- else
- {
- for(i = k; i <= m; i++)//第i个数分别与它后面的数字交换就能得到新的排列
- {
- if (swap(s, k, i))
- {
- Swap(s + k, s + i);
- AllRange(s, k +1, m);
- Swap(s + k, s + i);
- }
- }
- }
- }
- voidperm(char*s)
- {
- AllRange(s, 0, strlen(s) -1);
- }
- int main()
- {
- chars[] ="1123";
- printf("%s的全排列如下:\n", s);
- perm(s);
- return 0;
- }
来源: http://www.bubuko.com/infodetail-1990907.html