#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
char s[10];
while (scanf("%s", s) != EOF)
{
int n = strlen(s);
do {
puts(s);
} while (next_permutation(s, s + n));
printf("\n");
}
return 0;
}
2.dfs实现
#include <stdio.h>
#include <string.h>
char s[10];
bool visited[10];
int n;
void DFS(char a[], int len)
{
int i;
if (len == n)
{
a[n] = '\0';
printf(a);
printf("\n");
return;
}
for (i = 0; i < n; i++)
{
if (!visited[i])
{
visited[i] = true;
a[len] = s[i];
DFS(a, len + 1);
visited[i] = false;
}
}
}
int main()
{
while (scanf("%s", s) != EOF)
{
char a[10];
memset(visited, 0, sizeof(visited));
n = strlen(s);
DFS(a, 0);
printf("\n");
}
return 0;
}