// 全排列问题.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"
#include <iostream> using namespace std; template<class T>
void swap(T *str1,T *str2)
{
T temp;
temp=*str1;
*str1=*str2;
*str2=temp;
} void permStr(char *str,int i)//字符串的全排列
{
//printf("%d",i);
if(i==strlen(str)-1)
printf("%s\n",str);
else
{
for(int j=i;j<strlen(str);j++)
{
//printf("i %d,j %d",i,j);
swap(&str[i],&str[j]);
permStr(str,i+1);
swap(&str[i],&str[j]);
}
}
} void permInt(int a[],int n, int k)//数组全排列
{
if (k== n-1)
{
for (int i = 0; i < n; i ++)
cout<<a[i]<<" ";
cout<<endl;
}
else
{
for (int j = k; j < n; j ++)
{
swap(&a[j],&a[k]);
permInt(a,n,k+1);
swap(&a[j],&a[k]);
} }
} /*另外一个问题方法相似,一个数由1-9这10个数字组成。前i个数组成的数能被i整除*/
bool used[10]; void dfs(int a, int k)
{
if (k && (a%k) !=0)
return;
if (k==9)
{
cout<<a<<" ";
return;
}
else
{
for (int i = 1; i <= 9; i ++)
{
if (!used[i])
{
used[i] = 1;
dfs(a*10+i,k+1);
used[i] = 0;
}
}
} } int main()
{
char str[]={"abc"};
permStr(str,0); int a[]={1,2,3};
permInt(a,3,0); dfs(0,0); return 0; }

最新文章

  1. 算法系列(1):Google方程式
  2. Web Api 简介
  3. Quartz定时调度配置
  4. centos配置vpn服务器
  5. 如何系统地自学一门Python 语言(转)
  6. jquery 进阶
  7. 去除项目中的SVN标记
  8. Octal Fractions
  9. 分享一些无特征PHP一句话
  10. July 16th, Week 29th Saturday, 2016
  11. post&amp;get请求总结
  12. ios7.1 在线安装 失败的解决办法
  13. CDH ecosystem components
  14. 微软企业库Microsoft Enterprise Library的相关文章链接
  15. Apache Hadoop最佳实践和反模式
  16. Svn入门
  17. OpenRisc-32-ORPSoC烧写外部spi flash
  18. 初探linux子系统集之led子系统(三)
  19. Win32编程之静态库编写与使用.动态链接库的编写与使用
  20. JS 自己实现Map

热门文章

  1. Hibernate框架后续
  2. [Immutable.js] Transforming Immutable Data with Reduce
  3. 前端编辑神器Brackets
  4. OMXCodec与OMX事件处理流程
  5. maven:安装m2eclipse插件
  6. HTML 表格入门
  7. Oracle11g的注册表清理
  8. Entity Framework Code First主外键关系映射约定
  9. Ubutu命令 笔记积累
  10. 关于Ajax技术中servlet末尾的输出流