一 全排列
生成比如“123”的所有排列 123 213 231 321 312 132。
二 代码
#include <stdio.h>
#include <string.h>
void swap(char *a, char *b)
{
char tmp = *a;
*a = *b;
*b = tmp;
}
void per(char* s, char* pos)
{
if (*pos == '\0')
{
printf("%s\n", s);
}
else
{
char *p;
for (p = pos; *p != '\0'; ++p)
{
swap(pos, p);
per(s, pos+1);
swap(p, pos);
}
}
}
int main()
{
char s[100] = "1234";
per(s, s);
return 0;
}