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

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

#include<bits/stdc++.h>
using namespace std;
template<class Type>
void Perm(Type num[],int l,int r)
{
if(l==r)
{
for(int i=;i<=r;i++) cout<<num[i]<<" ";
cout<<endl;
}
else
{
for(int i=l;i<=r;i++)
{
swap(num[l],num[i]);
Perm(num,l+,r);
swap(num[l],num[i]);
}
}
}
int main()
{
int num[]={,,,,,,}; Perm(num,,);
cout<<endl; Perm(num,,);
cout<<endl;
}

看一下运行结果:

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

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

#include<bits/stdc++.h>
using namespace std;
void Perm(int num[],int st,int ed,int l,int r)//st,ed表示选取的范围; l,r表示进行全排列的范围
{
if(l==r)
{
for(int i=st;i<=ed;i++) cout<<num[i]<<" ";
cout<<endl;
return;
} for(int i=l;i<=r;i++)
{
swap(num[l],num[i]);
Perm(num,st,ed,l+,r);
swap(num[l],num[i]);
}
}
int main()
{
int num[]={,,,,,,}; Perm(num,,,,);
cout<<endl; Perm(num,,,,);
cout<<endl;
}

求全排列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. Android开发-- 简单对话框

    final Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.appicns_folder_sma ...

  2. Ansible 使用 Playbook 管理 Nginx 配置文件

    前面我们已经安装完 Nginx,但是在日常维护中经常需要修改配置文件,并重新加载配置文件,因此来写一个管理 Nginx 配置文件的 Playbook: [root@localhost ~]$ mkdi ...

  3. jq判断滚动条向上还是向下

    $(document).ready(function(){ ,t=; $(window).scroll(function(e){ p = $(this).scrollTop(); if(t<=p ...

  4. VC调用DLL

    VC调用DLL   调用DLL有两种方法:静态调用和动态调用. (一).静态调用其步骤如下: 1.把你的youApp.DLL拷到你目标工程(需调用youApp.DLL的工程)的Debug目录下; 2. ...

  5. JSP基本用法(一)运行机制和语法

    一.概述 JSP是一种建立在Servlet规范功能上的动态网页技术,在网页文件中嵌入Java代码和JSP标记用于产生动态内容. 本文简单介绍JSP的运行机制和JSP的语法. 二.JSP的运行机制 JS ...

  6. EPON ONU软件升级的若干优化方案

    1 说明 目前EPON ONU软件升级主要有IP方式(如SNMP/TR069)和TFTP+OAM两种.前者需占用大量IP地址,且配置ONU的IP地址需要手工操作,给业务开通和系统维护带来较大不便:后者 ...

  7. 【cs229-Lecture3】为什么要选择“最小二乘法”这个指标

    视频地址:http://v.163.com/movie/2008/1/E/B/M6SGF6VB4_M6SGHM4EB.html 具体的推导过程,讲义上都有,已经很详细了.这里的推导过程大都是自己为了练 ...

  8. 【大数据系列】hadoop集群的配置

    一.hadoop的配置文件分类 1.只读类型的默认文件 core-default.xml     hdfs-default.xml    mapred-default.xml   mapred-que ...

  9. jQuery Sizzle选择器(二)

    自己开始尝试读Sizzle源码.   1.Sizzle同过自执行函数的方式为自己创建了一个独立的作用域,它可以不依赖于jQuery的大环境而独立存在.因此它可以被应用到其它js库中.实现如下:(fun ...

  10. 升级后重启造成fsck.ext3: Unable to resolve UUID

    这篇文章帮了我的大忙了:转载自:http://wilber82.blog.51cto.com/1124820/724472 今天在做服务器补丁部署,有一台ESX4.1的服务器在升级后重启过程中挂了,通 ...