全排列
2016-06-20 22:17
330 查看
http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
http://www.cs.utexas.edu/users/djimenez/utsa/cs3343/lecture25.html
http://stackoverflow.com/questions/9148543/program-to-print-permutations-of-given-elements
http://www.cnblogs.com/guxuanqing/p/5904092.html
非常感谢作者!
针对含有重复串的更精简的方法:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main ()
{
char str[] = "ABA";
int len = strlen (str);
void permute (char *, int, int);
permute (str, 0, len - 1);
return 0;
}
void permute (char *A, int l, int r)
{
void swap (char *, int, int);
int i;
if (l == r) // Only 1 char left, print the permutation
{
printf ("%s\n", A);
}
else
{
for (i = l; i <= r; i++)
{
if (i == l)
permute (A, l + 1, r);
else if (A[l] == A[i])
{
continue;
}
else
{
swap (A, l, i);
permute (A, l + 1, r);
swap (A, l, i);
}
}
}
}
void swap (char *A, int l, int i)
{
int tmp = A[l];
A[l] = A[i];
A[i] = tmp;
}
http://www.cs.utexas.edu/users/djimenez/utsa/cs3343/lecture25.html
http://stackoverflow.com/questions/9148543/program-to-print-permutations-of-given-elements
http://www.cnblogs.com/guxuanqing/p/5904092.html
非常感谢作者!
针对含有重复串的更精简的方法:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main ()
{
char str[] = "ABA";
int len = strlen (str);
void permute (char *, int, int);
permute (str, 0, len - 1);
return 0;
}
void permute (char *A, int l, int r)
{
void swap (char *, int, int);
int i;
if (l == r) // Only 1 char left, print the permutation
{
printf ("%s\n", A);
}
else
{
for (i = l; i <= r; i++)
{
if (i == l)
permute (A, l + 1, r);
else if (A[l] == A[i])
{
continue;
}
else
{
swap (A, l, i);
permute (A, l + 1, r);
swap (A, l, i);
}
}
}
}
void swap (char *A, int l, int i)
{
int tmp = A[l];
A[l] = A[i];
A[i] = tmp;
}
相关文章推荐
- 倒计时CountDownTimer
- java 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
- ACM:蓝桥杯:笨小熊
- MySQL及SQL语句笔记
- 圆圈中最后剩下的数字46
- svn: Can't convert string from 'UTF-8' to native encoding 的解决办法
- linux,centos下解压tar,gz,bz2,tgz,zip等
- iOS 【UIKit-导航控制器 UINavigationController】
- 图书商城中的UserServlet中的继承的BaseServlet的原理
- 内部类的访问
- jQuery 简介 111
- ORA-00600: internal error code, arguments: [kcratr_nab_less_than_odr]问题处理
- EXT3_DX_ADD_ENTRY: DIRECTORY INDEX FULL!
- R.arry.xxxx的使用、Intent连接问题
- GridView属性,布局的抽取,自定义属性,自定义控件
- test
- [uart]stty命令使用
- 堆排序的思想以及实现过程(链式存储)
- Android中ListView的具体应用
- Morphing 动画