是在教材(《计算机算法设计与分析(第4版)》王晓东 编著)上看见的关于求全排列的算法;

我们可以看一下书上怎么写的:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. template<class Type>
  4. void Perm(Type num[],int l,int r)
  5. {
  6. if(l==r)
  7. {
  8. for(int i=;i<=r;i++) cout<<num[i]<<" ";
  9. cout<<endl;
  10. }
  11. else
  12. {
  13. for(int i=l;i<=r;i++)
  14. {
  15. swap(num[l],num[i]);
  16. Perm(num,l+,r);
  17. swap(num[l],num[i]);
  18. }
  19. }
  20. }
  21. int main()
  22. {
  23. int num[]={,,,,,,};
  24.  
  25. Perm(num,,);
  26. cout<<endl;
  27.  
  28. Perm(num,,);
  29. cout<<endl;
  30. }

看一下运行结果:

显然,这个函数在功能实现上……存在一定的问题(虽然思路上没问题),所以……

我自己重新写了一个,也许以后可能用的到呢:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. void Perm(int num[],int st,int ed,int l,int r)//st,ed表示选取的范围; l,r表示进行全排列的范围
  4. {
  5. if(l==r)
  6. {
  7. for(int i=st;i<=ed;i++) cout<<num[i]<<" ";
  8. cout<<endl;
  9. return;
  10. }
  11.  
  12. for(int i=l;i<=r;i++)
  13. {
  14. swap(num[l],num[i]);
  15. Perm(num,st,ed,l+,r);
  16. swap(num[l],num[i]);
  17. }
  18. }
  19. int main()
  20. {
  21. int num[]={,,,,,,};
  22.  
  23. Perm(num,,,,);
  24. cout<<endl;
  25.  
  26. Perm(num,,,,);
  27. cout<<endl;
  28. }

求全排列Permutation的更多相关文章

  1. PermutationsUnique,求全排列,去重

    问题描述:给定一个数组,数组里面有重复元素,求全排列. 算法分析:和上一道题一样,只不过要去重. import java.util.ArrayList; import java.util.HashSe ...

  2. 算法竞赛入门经典 习题2-10 排列(permutation)

    习题2-10 排列(permutation) 用1,2,3,-,9组成3个三位数 abc, def, 和ghi,每个数字恰好使用一次,要求 abc:def:ghi = 1:2:3.输出所有解.提示:不 ...

  3. 60. Permutation Sequence(求全排列的第k个排列)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  4. [LeetCode]60. Permutation Sequence求全排列第k个

    /* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...

  5. permutation求全排列

    include <iostream> #include <string> using namespace std; void swap(char &c1, char & ...

  6. [Swift]LeetCode60. 第k个排列 | Permutation Sequence

    The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...

  7. [Swift]LeetCode567. 字符串的排列 | Permutation in String

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  8. 【康拓展开】及其在求全排列第k个数中的应用

    题目:给出n个互不相同的字符, 并给定它们的相对大小顺序,这样n个字符的所有排列也会有一个顺序. 现在任给一个排列,求出在它后面的第i个排列.这是一个典型的康拓展开应用,首先我们先阐述一下什么是康拓展 ...

  9. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

随机推荐

  1. 线程同步 –AutoResetEvent和ManualResetEvent

    上一篇介绍了通过lock关键字和Monitor类型进行线程同步,本篇中就介绍一下通过同步句柄进行线程同步. 在Windows系统中,可以使用内核对象进行线程同步,内核对象由系统创建并维护.内核对象为内 ...

  2. WebService之CXF

    一.配置环境变量(Windows系统下要重启) 1.JAVA_HOME即JDK安装路径bin上一级,java -version命令验证 2.CXF_HOME即cxf安装路径bin上一级,cxf解压包下 ...

  3. iOS开发--libxml/HTMLparser.h file not found 解决方法 (libxml.dylib错误处理)

    点击左边项目的根目录,再点击右边的Build Settings,手工输入文字:“Header search paths”,然后单击(或双击,点击弹出面板下面的“+”号进行添加)“ Header sea ...

  4. 使用 requests 发送 GET 请求

    基本用法: import requests req = requests.get("http://www.baidu.com/") //发起GET请求 print(req.text ...

  5. js控制滚动条的位置以及隐藏滚动条

    document.documentElement.style.overflow = 'hidden'; //隐藏横竖滚动条 window.scrollTo(0,document.body.scroll ...

  6. iOS UIImage:获取图片主色调

    本文转载至 http://www.wahenzan.com/a/mdev/ios/2015/0325/1677.html -(UIColor*)mostColor{ #if __IPHONE_OS_V ...

  7. ldap命令

    ldapadd  -x   进行简单认证 -D   用来绑定服务器的DN -h   目录服务的地址 -w   绑定DN的密码 -f   使用ldif文件进行条目添加的文件 -W 交互式输入DN的密码 ...

  8. 【docker】 centos7 安装docker

    1.Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 通过 uname -r 命令查看你当前的内核版本 unam ...

  9. Android 控制闪光灯

    首先闪光灯可以用android.hardware.camera来控制. 1.添加权限 <uses-permission android:name="android.permission ...

  10. laravel blade模板里调用路由方法重定向

    @if (Session::get('user') == NULL) {!!Redirect::to('login')!!} @endif or @if (Session::get('user') = ...