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;
}
下次一定存到博客上面来.,..不对呀...我记得存进来了呀??
需要注意的地方我都标记了
#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;
}
相关文章推荐
- 全排列(递归和字典序)
- Poj(2488),按照字典序深搜
- LeetCode-49. Group Anagrams (JAVA)按照字典序分组
- C++实现全排列(字典序)
- 输出字符串的所有全排列(递归法和非递归,非递归采用组合数学的字典序)
- 全排列——字典序
- 2-sat按照最小字典序输出可行解(hdu1814)
- POJ 1833 排列(字典序全排列、next_permutation()、copy+流迭代器)
- C# 参数按照ASCII码从小到大排序(字典序)
- poj 1731 字典序全排列
- 给定n个字符串,请对n个字符串按照字典序排列。
- 2-sat按照最小字典序输出可行解(hdu1814)
- 全排列(按字典序)
- 【codeforces 724D】【贪心】 Dense Subsequence 【一个字符串,按照一定的区间要求从中选出一些字符,使得这串字符的sort后字典序最小】
- 递归实现全排列(按字典序)
- ACM常用模板 字典序全排列
- 字典序全排列
- poj 2337 欧拉回路按照最小字典序输出+注意为了按最小字典序怎么处理邻接表
- 今天开始学Java 给定n个字符串,请对n个字符串按照字典序排列。
- 算法练习 - 字符串的全排列(字典序排列)