求全排列Permutation
是在教材(《计算机算法设计与分析(第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的更多相关文章
- PermutationsUnique,求全排列,去重
问题描述:给定一个数组,数组里面有重复元素,求全排列. 算法分析:和上一道题一样,只不过要去重. import java.util.ArrayList; import java.util.HashSe ...
- 算法竞赛入门经典 习题2-10 排列(permutation)
习题2-10 排列(permutation) 用1,2,3,-,9组成3个三位数 abc, def, 和ghi,每个数字恰好使用一次,要求 abc:def:ghi = 1:2: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 ...
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- permutation求全排列
include <iostream> #include <string> using namespace std; void swap(char &c1, char & ...
- [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 ...
- [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 ...
- 【康拓展开】及其在求全排列第k个数中的应用
题目:给出n个互不相同的字符, 并给定它们的相对大小顺序,这样n个字符的所有排列也会有一个顺序. 现在任给一个排列,求出在它后面的第i个排列.这是一个典型的康拓展开应用,首先我们先阐述一下什么是康拓展 ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
随机推荐
- IOS指纹识别调用
最近正在开发的一个app需要加入指纹识别的功能,先搜索一下找到官方文档,简单易懂: https://developer.apple.com/library/ios/documentation/Loca ...
- Ubuntu 14.04服务器安装及软件配置
1.安装操作系统,配置root账号,通过sudo设置root的密码 如果使用ubuntu server 14.04,开启root需额外配置 1.开启root远程登录权限 sudo vi /etc/ss ...
- [转]PHP判断字符串是纯英文、纯汉字或汉英混合(GBK)
PHP判断字符串是否为中文(或英文)的方法,除了正则表达式判断和拆分字符判断字符的值是否小于128 外还有一种比较特别的方法. 使用php中的mb_strlen和strlen函数判断 方法比较简单:分 ...
- 【Python】Docx解析
1.cd D:\ProgramData\Anaconda3 2.pip install python-docx 3.python代码处理 # -*- coding: utf-8 -*- import ...
- Data Guard 主备库角色转换
1. switchover操作 1.1 备库先关闭实时日志应用 standby>alter database recover managed standby database cancel; 1 ...
- 【代码审计】LaySNS_v2.2.0 前台XSS跨站脚本漏洞
0x00 环境准备 LaySNS官网:http://www.laysns.com/ 网站源码版本:LaySNS_v2.2.0 程序源码下载:https://pan.lanzou.com/i0l38 ...
- Vim 手记:语法高亮
本文覆盖范围: Vim 的着色方案 设置高亮 选择颜色 语法高亮除错 每个程序员的文本编辑器缺少了语法高亮.特殊关键字和短语着色,都是不完整的.语法高亮突出了文档的结构,帮助发现排字错误,利于调试,整 ...
- STL——空间配置器(SGI-STL)
一. 空间配置器标准接口 参见<STL源码剖析>第二章-2.1.<memory>文件. 二.具备次配置力的SGI空间配置器 1. SGI STL的配置器与众不同,也与标准规范不 ...
- js控制滚动条的位置以及隐藏滚动条
document.documentElement.style.overflow = 'hidden'; //隐藏横竖滚动条 window.scrollTo(0,document.body.scroll ...
- WP8.1学习系列(第十二章)——全景控件Panorama开发指南
2014/6/18 适用于:Windows Phone 8 和 Windows Phone Silverlight 8.1 | Windows Phone OS 7.1 全景体验是本机 Windows ...