您的位置:首页 > 其它

7.2.1(按照字典序全排列)

2013-02-06 20:27 169 查看
诶诶诶,,,又忘了全排列的模版函数了,,,晕死了..忘了存到哪里去了,,呜呜,,

下次一定存到博客上面来.,..不对呀...我记得存进来了呀??

需要注意的地方我都标记了

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>

using namespace std;

const int maxn = 11111;

int n;

int a[maxn];

void Print_permutation(int *a, int cur)
{
if (cur == n)
{
for (int i = 0; i < n; i++)
{
printf("%d",a[i]);
}
cout << endl;
return ;
}
for (int i = 1; i <= n; i++) // 保证从1----n是字典序排列.
{
int ok = 1;
for (int j = 0; j < cur; j++)
{
if (a[j] == i)
{
ok = 0;
}
} // 保证排列中没有重复的元素
if (ok)
{
a[cur] = i; //存到数组里面
Print_permutation(a, cur + 1); //递归求解
}
}
}

int main()
{
while (scanf("%d", &n) != EOF)
{
Print_permutation(a, 0);
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: