您的位置:首页 > 其它

九度oj 1120全排列

2014-04-24 17:07 204 查看
1。STL实现
#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
= '\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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: