- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- void swap(char *a, char *b)
- {
- char t = *a;
- *a = *b;
- *b = t;
- }
- void permutation(char *str,const int index, const int length)
- {
- if (index == length)
- {
- static int counter = 1;
- printf("第%d个全排列:%s\\n", counter++, str);
- }else
- {
- int i = index;
- for (i = index; i <= length; i++)
- {
- swap(str + index, str + i);
- permutation(str, index+1, length);
- swap(str + index, str + i);
- }
- }
- }
- void AllRange(char *str)
- {
- permutation(str, 0, strlen(str) -1);
- }
- int main()
- {
- char str[] = "123";
- AllRange(str);
- system("pause");
- }
- //该片段来自于http://www.codesnippet.cn/detail/291120137606.html
来源: http://www.codesnippet.cn/detail/291120137606.html